410 likes | 2.18k Views
** Python Certification Training: https://www.edureka.co/python-programming-certification-training **<br>This Edureka PPT on Exception Handling Tutorial covers all the important aspects of making use and working with Exceptions using Python. It establishes all of the concepts like explaining why we need exception handling, the process of exception handling and how to go about using it practically. <br><br>Agenda<br>Why need Exception Handling?<br>What is Exception Handling?<br>Process of Exception Handling<br>Coding with Python<br>Try and Except block in Python<br>The else clause<br>The finally clause<br>Summary<br><br>Python Tutorial Playlist: https://goo.gl/WsBpKe<br>Blog Series: http://bit.ly/2sqmP4s<br><br>Instagram: https://www.instagram.com/edureka_lea...<br>Facebook: https://www.facebook.com/edurekaIN/ <br>Twitter: https://twitter.com/edurekain <br>LinkedIn: https://www.linkedin.com/company/edureka
E N D
Python Certification Training www.edureka.co/python
Exception Handling In Python Agenda Python Certification Training www.edureka.co/python
Agenda 01 Introduction Why need Exception Handling? 02 Getting Started What are exceptions? 03 Concepts Process of Exception Handling 04 Practical Approach Looking at code to understand theory Python Certification Training www.edureka.co/python
Exception Handling In Python Why Need Exception Handling? Python Certification Training www.edureka.co/python
Why Need Exception Handling? Dividing by zero Kid Programmer Python Certification Training www.edureka.co/python
Why Need Exception Handling? Dividing by zero Beautiful Error Message! Traceback (most recent call last): File, line 1, in <module> ZeroDivisionError: integer division or modulo by zero Python Certification Training www.edureka.co/python
Exception Handling In Python What Is Exception Handling? Python Certification Training www.edureka.co/python
What Is Exception Handling? Let us begin! What is an exception? An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions Definition of Exception What is exception handling? Process of responding to the occurrence, during computation, of exceptional conditions requiring special processing –often changing the normal flow of program execution Exception Handling Python Certification Training www.edureka.co/python
Exception Handling In Python Process of Exception Handling Python Certification Training www.edureka.co/python
Process of Exception Handling User Made Mistake If you can’t, Python will! Find Error “Try” Take Caution User Finds Anomaly Python Finds Anomaly “Catch” Fix Error Yes No Fixable? How? Everything is fixable! Handle the Exception I love Python! Python Certification Training www.edureka.co/python
Process of Exception Handling Important Terms try Keyword used to keep the code segment under check except Segment to handle the exception after catching it else Run this when no exceptions exist finally No matter what run this code if/if not for exception Python Certification Training www.edureka.co/python
Process of Exception Handling Visually it looks like this! Python Certification Training www.edureka.co/python
Exception Handling In Python Coding In Python Python Certification Training www.edureka.co/python
Coding In Python Python code for Exception Handling >>> print( 0 / 0 )) File "<stdin>", line 1 print( 0 / 0 )) >>> print( 0 / 0) Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero ^ SyntaxError: invalid syntax Syntax Error Exception Python Certification Training www.edureka.co/python
Raising An Exception We can use raise to throw an exception if a condition occurs x = 10 if x > 5: raise Exception('x should not exceed 5. The value of x was: {}'.format(x)) Traceback (most recent call last): File "<input>", line 4, in <module> Exception: x should not exceed 5. The value of x was: 10 Python Certification Training www.edureka.co/python
AssertionError Exception Instead of waiting for a program to crash midway, you can also start by making an assertion in Python import sys assert ('linux' in sys.platform), "This code runs on Linux only." Traceback (most recent call last): File "<input>", line 2, in <module> AssertionError: This code runs on Linux only. Python Certification Training www.edureka.co/python
Exception Handling In Python Try and Except Block Python Certification Training www.edureka.co/python
Try And Except Block The try and except block in Python is used to catch and handle exceptions def linux_interaction(): assert ('linux' in sys.platform), "Function can only run on Linux systems." print('Doing something.') try: linux_interaction() except: pass Try it out! Python Certification Training www.edureka.co/python
Try And Except Block The program did not crash! try: linux_interaction() except: print('Linux function was not executed') Linux function was not executed Python Certification Training www.edureka.co/python
Try And Except Block Example where you capture the AssertionError and output message try: linux_interaction() except AssertionError as error: print(error) print('The linux_interaction() function was not executed') Function can only run on Linux systems. The linux_interaction() function was not executed Python Certification Training www.edureka.co/python
Try And Except Block Another example where you open a file and use a built-in exception try: with open('file.log') as file: read_data = file.read() except: print('Could not open file.log') If file.log does not exist, this block of code will output the following: Could not open file.log Python Certification Training www.edureka.co/python
Try And Except Block In the Python docs, you can see that there are a lot of built-in exceptions that you can use Exception FileNotFoundError Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT. try: with open('file.log') as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error) If file.log does not exist, this block of code will output the following: [Errno 2] No such file or directory: 'file.log' Python Certification Training www.edureka.co/python
Try And Except Block Another Example Case try: linux_interaction() with open('file.log') as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error) except AssertionError as error: print(error) print('Linux linux_interaction() function was not executed') If the file does not exist, running this code on a Windows machine will output the following: Function can only run on Linux systems. Linux linux_interaction() function was not executed [Errno 2] No such file or directory: 'file.log' Python Certification Training www.edureka.co/python
Try And Except Block Key Takeaways • A try clause is executed up until the point where the first exception is encountered. • Inside the except clause, or the exception handler, you determine how the program responds to the exception. • You can anticipate multiple exceptions and differentiate how the program should respond to them. • Avoid using bare except clauses. Python Certification Training www.edureka.co/python
Exception Handling In Python The else Clause Python Certification Training www.edureka.co/python
The else Clause Using the else statement, you can instruct a program to execute a certain block of code only in the absence of exceptions Python Certification Training www.edureka.co/python
The else Clause Example Case try: linux_interaction() except AssertionError as error: print(error) else: print('Executing the else clause.') Doing something. Executing the else clause. Python Certification Training www.edureka.co/python
The else Clause You can also try to run code inside the else clause and catch possible exceptions there as well: try: linux_interaction() except AssertionError as error: print(error) else: try: with open('file.log') as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error) Doing something. [Errno 2] No such file or directory: 'file.log' Python Certification Training www.edureka.co/python
Exception Handling In Python The finally Clause Python Certification Training www.edureka.co/python
The finally Clause finally is used for cleaning up! Python Certification Training www.edureka.co/python
The finally Clause finally is used for cleaning up! try: linux_interaction() except AssertionError as error: print(error) else: try: with open('file.log') as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error) finally: print('Cleaning up, irrespective of any exceptions.') Function can only run on Linux systems. Cleaning up, irrespective of any exceptions. Python Certification Training www.edureka.co/python
Exception Handling In Python Summary Python Certification Training www.edureka.co/python
Summary In this session, we covered the following topics: • raiseallows you to throw an exception at any time. • assert enables you to verify if a certain condition is met and throw an exception if it isn’t. • In the tryclause, all statements are executed until an exception is encountered. • exceptis used to catch and handle the exception(s) that are encountered in the try clause. • elselets you code sections that should run only when no exceptions are encountered in the try clause. • finallyenables you to execute sections of code that should always run, with or without any previously encountered exceptions. Python Certification Training www.edureka.co/python
Summary Python Programming, yay! Python Certification Training www.edureka.co/python