Exception Handling

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Exception Handling

Class 12th
Error

Different types of exceptions in Python

Exception

Error Vs Exception

Topics Program without and with Exception


Handling

Built-in exceptions in python

Handling multiple exceptions

Finally Block
Python: Exception
Handling

2024 Exception handling 3


Error
Errors are problems in a program due to which will stop the program from
execution.
Errors in a program can be categorized into following types:
1. Compile Time Error: Errors that occur during the compilation of the
program.
2. Run Time Error: Errors that occur while the program is running.
Some of the example of run time error are:
•Division by Zero
•Using of undefined variable
•Trying to access a non existing file

2024 Exception handling 4


Error
3. Logical Error: Errors in the program’s logic, which do not produce
errors but lead to incorrect results. Some of the example of logical error
are:
•Giving wrong operator precedence
•using wrong variable name for calculation
4. Syntax Errors: Mistakes in writing code that prevent the program from
running. Some of the example of syntax error are:
•Incorrect Indentation
•Misspelled a keyword
•leaving out a symbol such as colon (:), parentheses [{()}] or comma (,).
5. Semantics Errors: Logic errors that produce unintended results.
2024 Exception handling 5
Exception
• An exception is a type of error that occurs during
program execution.
• Python generates an exception whenever an error is
encountered.
• Exceptions can be handled to prevent the program from
crashing.

2024 Exception handling 6


Error vs. Exception
• Error: Any mistake or issue in the code.
• Exception: An unexpected situation or error during program execution.

2024 Exception handling 7


Exception Handling
• Exception handling is the process of dealing with run-time errors.
• It involves using `try…except` blocks to catch and handle exceptions.

2024 Exception handling 8


Different types of exceptions in python
1. `SyntaxError`: Raised when there is a syntax error in your code.
2. `IndentationError`: Raised when there is an indentation error in your code,
such as mismatched indentation levels.
3. `NameError`: Raised when a local or global name is not found.
4. `TypeError`: Raised when an operation or function is applied to an object
of inappropriate type.
5. `ValueError`: Raised when an operation or function receives an argument
of the correct type but an inappropriate value.
6. `KeyError`: Raised when a dictionary is accessed with a key that doesn’t
exist.
7. `IndexError`: Raised when trying to access an element at an invalid index
in a sequence (e.g., list, tuple).
2024 Exception handling 9
Different types of exceptions in python
8. `FileNotFoundError`: Raised when trying to open or manipulate a file that does not
exist.
9. `IOError`: Raised for I/O-related errors, such as when reading or writing to a file
fails.
10. `ZeroDivisionError`: Raised when attempting to divide by zero.
11. `AttributeError`: Raised when an attribute reference or assignment fails.
12. `ImportError`: Raised when an import statement fails to find the module.
13. `KeyboardInterrupt`: Raised when the user interrupts the program (e.g., by
pressing Ctrl+C).

2024 Exception handling 10


Different types of exceptions in python
14. `EOFError`: Raised when an input operation reaches the end of the file.
15. `ArithmeticError`: A base class for numeric errors, including `ZeroDivisionError`
and `OverflowError`.
16. `FileExistsError`: Raised when trying to create a file or directory that already
exists.
17. `PermissionError`: Raised when trying to perform an operation without the
necessary permissions.

2024 Exception handling 11


Program without
Exception Handling

2024 Exception handling 12


2024 Exception handling 13
Program with Exception
Handling

2024 Exception handling 14


2024 Exception handling 15
Handling Multiple
Exceptions

2024 Exception handling 16


2024 Exception handling 17
2024 Exception handling 18
Handling Exceptions with
else block

2024 Exception handling 19


2024 Exception handling 20
The `finally` Block

2024 Exception handling 21


The `finally` Block
In programming, there may be some situation in which the current method ends up while handling some
exceptions. But the method may require some additional steps before its termination, like closing a file
or a network and so on.

So, in order to handle these situations, Python provides a keyword finally, which is always executed
after try and except blocks. The finally block always executes after normal termination of try block or
after try block terminates due to some exception.

2024 Exception handling 22


2024 Exception handling 23
2024 Exception handling 24
Raising Exceptions
The raise statement allows the programmer to force a specific exception to
occur. The sole argument in raise indicates the exception to be raised.

2024 Exception handling 25


2024 Exception handling 26
Advantages of Exception Handling
•Improved program reliability: By handling exceptions properly, you can
prevent your program from crashing or producing incorrect results due to
unexpected errors or input.
•Simplified error handling: Exception handling allows you to separate error
handling code from the main program logic, making it easier to read and
maintain your code.
•Cleaner code: With exception handling, you can avoid using complex
conditional statements to check for errors, leading to cleaner and more readable
code.
•Easier debugging: When an exception is raised, the Python interpreter prints a
traceback that shows the exact location where the exception occurred, making it
easier to debug your code.

2024 Exception handling 27


Disadvantages of Exception Handling
•Performance overhead: Exception handling can be slower than using
conditional statements to check for errors, as the interpreter has to
perform additional work to catch and handle the exception.
•Increased code complexity: Exception handling can make your code
more complex, especially if you have to handle multiple types of
exceptions or implement complex error handling logic.
•Possible security risks: Improperly handled exceptions can potentially
reveal sensitive information or create security vulnerabilities in your
code, so it’s important to handle exceptions carefully and avoid
exposing too much information about your program.

2024 Exception handling 28


thank you
Ramneek Kour
Computer Science

You might also like