0% found this document useful (0 votes)
4 views57 pages

Understanding Python Exception Handling

The document outlines different types of errors in programming, including syntax errors, logical errors, and runtime errors, with examples for each. It also explains how to handle exceptions in Python using try-except statements, including variations with else and finally blocks. These methods allow for graceful error handling and cleanup in code execution.

Uploaded by

mruthunjayk11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views57 pages

Understanding Python Exception Handling

The document outlines different types of errors in programming, including syntax errors, logical errors, and runtime errors, with examples for each. It also explains how to handle exceptions in Python using try-except statements, including variations with else and finally blocks. These methods allow for graceful error handling and cleanup in code execution.

Uploaded by

mruthunjayk11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exception Handling

Types of Errors
(a) Syntax Error:
• 1. Incorrect indentation
• 2. Misspelling a keyword
• 3. Leaving out a symbol such as colon (:), comma (,) or parentheses
(()).
Logical error
Some examples of logical errors are:
• Using the wrong variable name for calculations.
• Using integer division or modulus operator in place of division
operator
• Giving wrong operator precedence.
Runtime Error (exception)
• You know you have a run-time error when the application suddenly
stops running and displays an error (exception) dialog box or when
the user complains about erroneous output. It usually results in
abnormal program termination during execution.
1. Division by zero
2. Performing an operation on incompatible types
3. Using an identifier variable which has not been defined
4. Accessing a list element, dictionary value or object attribute which
doesn’t exist
5. Trying to access a file that doesn’t exist
Built In Exceptions
• You can use the try-except statement to gracefully handle exceptions.
Here are the different methods for handling exceptions in Python:
Method 1: try and except block
• Syntax:
Method 2: try...except with else block
• The else clause of try statement is used to specify the code which is
executed in case no exception occurs.
In the above example, the else block is executed only when none of the exceptions is
executed. Whenever an error occurs, the required exception is executed and else block is
not executed.
Method 3: try...except with finally block
The finally block is called clean-up or termination clause because it is
executed under all circumstances, i.e., a “finally” block is always
executed irrespective of whether an exception has occurred in a try
block or not.

You might also like