Computer science | Informatics Practices CUET 2023
Exception Handling in Python
Syntax errors:
● Syntax errors are errors that occur due to incorrect syntax in the code.
● These errors occur during compilation and prevent the code from being
executed.
● Examples of syntax errors include missing or incorrect punctuation,
misspelled keywords, or incorrect indentation.
Exceptions:
● Exceptions are errors that occur during the execution of a program.
● These errors are not necessarily caused by syntax errors, but rather
unexpected conditions or inputs.
● Examples of exceptions include division by zero, accessing an array out of
bounds, or trying to open a file that does not exist.
Need of exception handling:
● Exception handling is necessary to handle unexpected errors and prevent
the program from crashing.
● Without exception handling, the program would terminate whenever an
exception occurs, making it difficult to diagnose and fix the issue.
User-defined exceptions:
● User-defined exceptions are exceptions that are created by the
programmer.
● These exceptions can be used to handle specific errors that are not
covered by built-in exceptions.
Raising exceptions:
● Exceptions can be raised using the 'raise' keyword.
● This allows the programmer to intentionally trigger an exception under
certain conditions.
Lovejeet Arora
Computer science | Informatics Practices CUET 2023
2
Handling exceptions:
● Exceptions can be handled using the 'try' and 'except' blocks.
● The 'try' block contains the code that may raise an exception, and the
'except' block specifies how to handle the exception if it occurs.
Catching exceptions:
● Exceptions can be caught using the 'except' block.
● The 'except' block contains the code that is executed if an exception
occurs in the 'try' block.
Try - except - else clause:
● The 'try-except-else' clause allows the programmer to execute code that
should only run if no exceptions occur.
● The 'else' block is executed if no exceptions occur in the 'try' block.
Try - finally clause:
● The 'try-finally' clause allows the programmer to execute code that should
always run, regardless of whether an exception occurs or not.
● The 'finally' block is always executed, even if an exception is raised and
caught in the 'try' block.
Recovering and continuing with finally:
● The 'finally' block can be used to recover from an exception and continue
with the program.
● This can be useful in situations where the program needs to perform
certain actions before terminating.
Built-in exception classes:
● Python provides a number of built-in exception classes that can be used to
handle common types of exceptions.
● Examples of built-in exception classes include 'TypeError', 'ValueError',
and 'IndexError'.
Best practices for exception handling:
● Exception handling should be used sparingly and only for exceptional
circumstances.
● Exceptions should be raised early and caught late.
Lovejeet Arora
Computer science | Informatics Practices CUET 2023
3
● Exceptions should be specific and provide useful information about the
error.
● Exception handling should be tested thoroughly to ensure that it works as
intended.
⭐⭐⭐⭐
Lovejeet Arora