Explain. Exception handling with example.
Question
Explain. Exception handling with example.
Solution 1
Exception handling is a mechanism in programming to handle runtime errors, so that normal flow of the program can be maintained. It is a process to handle the runtime errors so that normal flow of the application can be maintained.
Here is a simple example in Python:
try:
# code block where exception can occur
a = 1
b = "s"
c = a + b
except Exception as e:
# what to do when exception occurred, print error message
print("An error occurred. Error message: ", str(e))
In this example, we are trying to add a string to an integer, which will raise a TypeError. This error is caught in the except block and a custom error message is printed.
Here's how it works:
- The code within the
tryblock is executed statement by statement. - If no exception occurs, the
exceptblock is skipped and execution of thetrystatement is finished. - If an exception occurs during execution of the
tryblock, the rest of the block is skipped. Then if its type matches the exception named after theexceptkeyword, theexceptblock is executed, and then execution continues after thetrystatement. - If an exception occurs which does not match the exception named in the
exceptclause, it is passed on to outertrystatements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
Solution 2
Exception handling is a mechanism in programming to handle runtime errors, so that normal flow of the program can be maintained. It is a process to handle the runtime errors so that normal flow of the application can be maintained.
Here is a simple example in Python:
try:
# code block where exception can occur
a = 1
b = "s"
c = a + b
except Exception as e:
# what to do in case of exception
print("An error occurred: ", e)
In this example, we are trying to add a string to an integer, which will raise a TypeError. This error is caught by the except block, and a message is printed to the console.
Here's how it works:
- The code within the
tryblock is executed statement by statement. - If no exception occurs, the
exceptblock is skipped and execution of thetrystatement is finished. - If an exception occurs during execution of the
tryblock, the rest of the block is skipped. Then if its type matches the exception named after theexceptkeyword, theexceptblock is executed, and then execution continues after thetrystatement. - If an exception occurs which does not match the exception named in the
exceptclause, it is passed on to outertrystatements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
Similar Questions
Explain Java’s built-in exceptions and its respective meaning.
Explain Java Exception and its types.
Explain the following terms w.r.t exception handling i) Try ii) catch iii) throw iv) fina
exception handling in java
Describe two types of exceptions in the context of java programming
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.