0% found this document useful (0 votes)
9 views10 pages

Python Exception Handling Basics

The document explains exceptions in programming, which are errors detected during execution that disrupt the normal flow of instructions. It details built-in exception types in Python, such as SyntaxError and ZeroDivisionError, and emphasizes the importance of exception handling to prevent abrupt program termination. Additionally, it discusses user-defined exceptions for handling specific constraints in real-life applications.

Uploaded by

meghajain.cs24
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)
9 views10 pages

Python Exception Handling Basics

The document explains exceptions in programming, which are errors detected during execution that disrupt the normal flow of instructions. It details built-in exception types in Python, such as SyntaxError and ZeroDivisionError, and emphasizes the importance of exception handling to prevent abrupt program termination. Additionally, it discusses user-defined exceptions for handling specific constraints in real-life applications.

Uploaded by

meghajain.cs24
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

Exceptions

•Errors detected during execution of programs are called


exceptions. Even if a statement or expression is syntactically
correct, it may cause an error when an attempt is made to
execute it. An exception is an event raised that disrupts the
normal flow of the program's instructions.

•Situations where exceptions can occur:


• Division by 0
• Opening a file
• Index out of bound ---- etc.
• Example --
Built-in Exception types in Python
• Python has a number of built-in exceptions that are derived from the
Exception class.
• Some of them are listed below:
1. SyntaxError: This exception is raised when a syntax error is encountered
in the code, such as a missing colon, wrong keyword etc.
2. TypeError: This exception is raised when an operation or function is
applied to a wrong type, such as dividing 2 strings.
3. ZeroDivisionError: This exception is raised when we try to divide a
number by 0.
4. KeyError: This exception is raised when a key is not found in the
dictionary.
5. NameError: When a function is not found in the current scope.
6. AttributeError: When an attribute or method is not found on an
object.
7. ImportError: This exception occurs when an import statement
fails to find or load the module.
8. IndexError: When the index is out of range for the collection
types.
Exception Handling
•Exception handling allows us to separate error-giving code from
normal code.
•The main advantage of exception handling is that it allows the
smooth ending of the programs when exceptions occur, instead
of abrupt ending.
•Suppose a program has 50 lines of code. If there is an exception
in the 5th line, the program abruptly stops at that line and the
rest of the lines of the code don’t run.
•By using exception handling, we restrict the error from causing
trouble to the other lines of code.
General syntax of try and except:

The code that might generate an Exception is placed in the try block.
The try block is followed by an except block. When an Exception
occurs, it is caught by the except block. The except block cannot be
used without the try block.
In the example shown below, note that the “rest of the program” runs
undisturbed whether or not the error occurs as we have carefully placed the
possible error lines within the try block.
Multiple except blocks allow us to handle each exception differently.

Run 1 Run 2
Other Exception examples(KeyError):

Run 1 Run 2
User Defined Exceptions
•When programs for real life applications in Python are written,
there are many constraints on the values which the variables can
take in the program. For example, age cannot have a negative value.
When a person inputs a negative value for age, the program should
show an error message.
•These type of errors cannot be caught by built-in exception types.
Hence, we need to define our own exception classes that derive
from the Exception class.
•The keyword raise is used to raise a specific exception.

You might also like