Computer Science

Explain catching exceptions using try and except block.

Python Exception Handling

9 Likes

Answer

An exception is said to be caught when a code that is designed to handle a particular exception is executed. Exceptions, if any, are caught in the try block and handled in the except block. While writing or debugging a program, a user might doubt an exception to occur in a particular part of the code. Such suspicious lines of codes are put inside a try block. Every try block is followed by an except block. The appropriate code to handle each of the possible exceptions (in the code inside the try block) are written inside the except clause. While executing the program, if an exception is encountered, further execution of the code inside the try block is stopped and the control is transferred to the except block. The syntax of try … except clause is as follows:

try:
    [ program statements where exceptions might occur]
except [exception-name]:
    [ code for exception handling if the exception-name error is encountered]

Answered By

4 Likes


Related Questions