Bird
Raised Fist0
Agentic AIml~20 mins

Error handling in tool calls in Agentic AI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of error handling in a tool call
What is the output of this code snippet that calls a tool and handles errors?
Agentic AI
def call_tool(tool, input_data):
    try:
        result = tool(input_data)
        return f"Success: {result}"
    except ValueError:
        return "ValueError caught"
    except Exception:
        return "General error caught"

def sample_tool(x):
    if x < 0:
        raise ValueError("Negative input")
    elif x == 0:
        raise RuntimeError("Zero input")
    else:
        return x * 2

output = call_tool(sample_tool, 0)
print(output)
ASuccess: 0
BValueError caught
CGeneral error caught
DRuntimeError: Zero input
Attempts:
2 left
💡 Hint
Think about which exception is raised and which except block catches it.
Model Choice
intermediate
2:00remaining
Choosing the right error handling model for tool calls
Which error handling model is best suited for a tool call that may raise multiple specific exceptions and needs to log errors distinctly?
AMultiple try-except blocks each catching one specific exception type separately
BSingle try-except block catching all exceptions with a general except clause
CTry-except with nested try blocks inside except clauses
DNo try-except, just let exceptions propagate to the caller
Attempts:
2 left
💡 Hint
Consider clarity and specific error handling needs.
🔧 Debug
advanced
2:00remaining
Identify the error in this tool call error handling code
What error will this code raise when calling the tool with input 5?
Agentic AI
def tool(x):
    if x == 5:
        raise KeyError("Key missing")
    return x

def call_tool(tool_func, val):
    try:
        return tool_func(val)
    except ValueError:
        return "Value error handled"
    except KeyError:
        return "Key error handled"
    except:
        return "Other error"

result = call_tool(tool, 5)
print(result)
ARaises an uncaught exception
BKey error handled
COther error
DValue error handled
Attempts:
2 left
💡 Hint
Check which except block matches the raised exception.
Hyperparameter
advanced
2:00remaining
Choosing retry count for error handling in tool calls
If a tool call fails due to a transient network error, which retry count is most reasonable to balance responsiveness and robustness?
A1 retry
B0 retries (fail immediately)
C10 retries
D100 retries
Attempts:
2 left
💡 Hint
Too many retries can cause delays; too few may miss recovery.
🧠 Conceptual
expert
3:00remaining
Best practice for error handling in asynchronous tool calls
Which approach best ensures error handling in asynchronous tool calls to avoid silent failures?
AUse callbacks without error handling
BIgnore errors and rely on the caller to handle exceptions
CWrap async calls in a synchronous try-except block
DUse try-except inside the async function and log errors before returning
Attempts:
2 left
💡 Hint
Think about where errors can be caught in async code.

Practice

(1/5)
1. What is the main purpose of using try-except blocks when calling external tools in an AI agent?
easy
A. To make the tool run in parallel
B. To speed up the tool's execution
C. To increase the tool's accuracy
D. To catch errors and prevent the program from crashing

Solution

  1. Step 1: Understand the role of try-except blocks

    Try-except blocks are used to catch errors that happen during code execution, especially when calling external tools that might fail.
  2. Step 2: Identify the benefit in AI agent tool calls

    By catching errors, the program avoids crashing and can handle failures gracefully, improving reliability.
  3. Final Answer:

    To catch errors and prevent the program from crashing -> Option D
  4. Quick Check:

    Error catching = Prevent crash [OK]
Hint: Try-except blocks catch errors to keep programs running [OK]
Common Mistakes:
  • Thinking try-except speeds up code
  • Confusing error handling with improving accuracy
  • Assuming try-except runs code in parallel
2. Which of the following is the correct syntax to catch a general error when calling a tool in Python?
easy
A. try: tool_call() error: handle_error()
B. try: tool_call() catch: handle_error()
C. try: tool_call() except: handle_error()
D. try: tool_call() fail: handle_error()

Solution

  1. Step 1: Recall Python error handling syntax

    Python uses try and except blocks to catch errors.
  2. Step 2: Match the correct keywords

    The correct keywords are try and except, not catch, error, or fail.
  3. Final Answer:

    try: tool_call() except: handle_error() -> Option C
  4. Quick Check:

    Python uses except, not catch [OK]
Hint: Remember Python uses except, not catch, for errors [OK]
Common Mistakes:
  • Using catch instead of except
  • Using error or fail as keywords
  • Missing indentation in try-except blocks
3. Consider this code snippet:
try:
  result = tool_call('data')
except Exception:
  result = 'Fallback result'
print(result)
If tool_call raises an error, what will be printed?
medium
A. 'Fallback result'
B. None
C. Nothing, program crashes
D. The error message from tool_call

Solution

  1. Step 1: Analyze the try-except behavior

    If tool_call raises an error, the except block runs and sets result to 'Fallback result'.
  2. Step 2: Understand the print output

    After the except block, print(result) prints the fallback string.
  3. Final Answer:

    'Fallback result' -> Option A
  4. Quick Check:

    Error caught = fallback printed [OK]
Hint: If error caught, except block runs fallback code [OK]
Common Mistakes:
  • Assuming error message prints automatically
  • Thinking program crashes despite except
  • Expecting None instead of fallback
4. This code tries to call a tool and handle errors:
try:
  output = tool_call()
except Exception as e
  print('Error:', e)
  output = None
print(output)
What is the error in this code?
medium
A. Using print inside except block is not allowed
B. Missing colon after except Exception as e
C. output should be set before try block
D. tool_call() must be inside a function

Solution

  1. Step 1: Check except syntax

    The except line is missing a colon at the end, which is required in Python syntax.
  2. Step 2: Verify other parts

    Printing inside except is allowed, output can be set there, and tool_call can be called anywhere.
  3. Final Answer:

    Missing colon after except Exception as e -> Option B
  4. Quick Check:

    Except line needs colon [OK]
Hint: Except lines always end with a colon ':' [OK]
Common Mistakes:
  • Forgetting colon after except
  • Thinking print is disallowed in except
  • Assuming output must be pre-set
5. You want your AI agent to call two tools in sequence. If the first tool fails, it should use a fallback result and still call the second tool. Which code correctly handles this?
hard
A. try: result1 = tool1() except Exception: result1 = 'fallback' result2 = tool2() print(result1, result2)
B. result1 = tool1() result2 = tool2() if not result1: result1 = 'fallback' print(result1, result2)
C. try: result1 = tool1() result2 = tool2() except Exception: result1 = 'fallback' result2 = 'fallback2' print(result1, result2)
D. try: result1 = tool1() except Exception: result1 = 'fallback' result2 = tool2() print(result1, result2)

Solution

  1. Step 1: Understand the requirement

    If the first tool fails, use fallback for result1 but still call tool2 normally.
  2. Step 2: Analyze each option

    try: result1 = tool1() except Exception: result1 = 'fallback' result2 = tool2() print(result1, result2) tries tool1, catches error to set fallback, then calls tool2 outside except, so tool2 always runs. result1 = tool1() result2 = tool2() if not result1: result1 = 'fallback' print(result1, result2) does not catch exceptions, so failure crashes. try: result1 = tool1() result2 = tool2() except Exception: result1 = 'fallback' result2 = 'fallback2' print(result1, result2) calls both tools inside try, so if tool2 fails, both fallback. try: result1 = tool1() except Exception: result1 = 'fallback' result2 = tool2() print(result1, result2) calls tool2 inside except, so tool2 runs only if tool1 fails.
  3. Final Answer:

    Option A correctly handles fallback and always calls second tool -> Option A
  4. Quick Check:

    Separate try-except for first tool, call second after [OK]
Hint: Put second tool call outside except to always run it [OK]
Common Mistakes:
  • Calling second tool only inside except block
  • Not catching exceptions for first tool
  • Putting both calls inside one try-except