exception handling Flashcards
is used to handle exceptions (errors) that may
arise during the execution of a program.
try — except block
types of try except block
- try-except
- except block
- catching all exception
- else with try except
- finally block
- raising exception
- custom exception
- exception chaining
is used to wrap the code that might throw an exception so if any error occurs with the try an error the python will immediately jump the correspond except block to handle that error.
try-except
is executed if an error occurs in the try block so you can specify the type of exception that you want to catch
except block
is you can catch all exceptions with a generic block by omitting the specific exception name. however, this should be used carefully since it can also catch unexpected errors making the debugging harder
catching all exceptions
else block can be used in conjunction with try-except code that could run only if no exception or raise
else with try - except
is used to execute code that should run no matter what whether an exception occurred or not it is often used to clean up operations like closing files or network connection
finally block
you can manually raise an exception in Python using the raise keyword. this is useful when you want to trigger an exception based on a specific condition okay specific condition
raising exception
you can Define your own exception types by creating a new class that inherits from the built-in exception class so this is useful for creating specific types of error in our programs
custom exception
python allows you to chain exceptions using the race from syntax which help in providing more context when one exception leads to another
exception chaining