⬅ Previous Topic
Types of Programming Errors - Syntax vs Runtime vs LogicNext Topic ⮕
Custom Exceptions⬅ Previous Topic
Types of Programming Errors - Syntax vs Runtime vs LogicNext Topic ⮕
Custom ExceptionsTry/Catch blocks are fundamental to error handling in programming. They allow you to gracefully handle errors that occur during the execution of your code, instead of letting your program crash unexpectedly.
The try
block contains code that might throw an error, and the catch
block defines how to handle that error if it occurs. This structure ensures your program remains in control even when something goes wrong.
try:
// code that may cause an error
catch error:
// code to handle the error
Let's consider a basic example where we divide two numbers, and we want to catch any division-by-zero errors.
try:
result = divide(10, 0)
print("Result is", result)
catch error:
print("An error occurred: Division by zero is not allowed")
An error occurred: Division by zero is not allowed
In the try
block, we attempt to divide 10 by 0. Since dividing by zero is invalid, an error occurs. Instead of crashing, the program jumps to the catch
block and prints a meaningful message.
What happens if no error occurs inside a try
block?
If no error occurs, the catch
block is skipped entirely, and the program continues normally.
Imagine prompting a user for input that must be converted to a number. If the input is not valid, it would cause an error.
try:
input_value = get_input("Enter a number:")
number = convert_to_integer(input_value)
print("Double the number is:", number * 2)
catch error:
print("Invalid input. Please enter a valid number.")
Output (if user enters 'abc'):
Invalid input. Please enter a valid number.
If the user enters something like "abc"
, the conversion to integer fails and throws an error. The catch
block handles it and provides a helpful message instead of crashing the program.
Can I have multiple catch blocks for different error types?
Yes, many programming languages allow multiple catch
blocks to handle specific error types separately. For example, one for division errors, another for file errors, etc.
try
block—not the entire program.catch
to guide users or developers.Sometimes, you may need to use a try/catch inside another try/catch to handle errors at different levels.
try:
try:
value = get_input("Enter number:")
number = convert_to_integer(value)
result = divide(100, number)
print("Result:", result)
catch inner_error:
print("Inner error:", inner_error)
catch outer_error:
print("Something went very wrong:", outer_error)
Output (if user enters '0'):
Inner error: Division by zero
try/catch
blocks make your programs more robust and user-friendly by safely managing errors. Whether it’s a division error, a file that doesn’t exist, or bad input—handling it correctly ensures your program doesn’t crash and provides a smoother experience for the user.
⬅ Previous Topic
Types of Programming Errors - Syntax vs Runtime vs LogicNext Topic ⮕
Custom ExceptionsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.