Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Raising and Handling Python Exceptions Show A Python program terminates as soon as it encounters an error. In Python, an error can be a syntax error or an exception. In this article, you will see what an exception is and how it differs from a syntax error. After that, you will learn about raising exceptions and making assertions. Then, you’ll finish with a demonstration of the try and except block. Exceptions versus Syntax ErrorsSyntax errors occur when the parser detects an incorrect statement. Observe the following example:
The arrow indicates where the parser ran into the syntax error. In this example, there was one bracket too many. Remove it and run your code again:
This time, you ran into an exception error. This type of error occurs whenever syntactically correct Python code results in an error. The last line of the message indicated what type of exception error you ran into. Instead of showing the message Raising an ExceptionWe can use If you want to throw an error when a certain condition occurs using
When you run this code, the output will be the following:
The program comes to a halt and displays our exception to screen, offering clues about what went wrong. The AssertionError ExceptionInstead of waiting for a program to crash midway, you can also start by making an assertion in Python. We Have a look at the following example, where it is asserted that the code will be executed on a Linux system:
If you run this code on a Linux machine, the assertion passes. If you were to run this code on a Windows machine, the outcome of the assertion would be
In this example, throwing an The try and except Block: Handling ExceptionsThe As you saw earlier, when syntactically correct code runs into an error, Python will throw an exception error. This exception error will crash the program if it is
unhandled. The The following function can help you understand the
The You can give the function a
The way you handled the error here is by handing
out a You got nothing. The good thing here is that the program did not crash. But it would be nice to see if some type of exception occurred whenever you ran your code. To this end, you can change the
Execute this code on a Windows machine:
When an exception occurs in a program running this function, the program will continue as well as inform you about the fact that the function call was not successful. What you did not get to see was the type of error that was thrown as a result of the function call. In order to see exactly what went wrong, you would need to catch the error that the function threw. The following code is an example where you capture the
Running this function on a Windows machine outputs the following:
The first message is the In the previous example, you called a function that you wrote yourself. When you executed the function, you caught the Here’s another example where you open a file and use a built-in exception:
If file.log does not exist, this block of code will output the following: This is an informative message, and our program will still continue to run. In the Python docs, you can see that there are a lot of built-in exceptions that you can use here. One exception described on that page is the following:
To catch this type of exception and print it to screen, you could use the following code:
In this case, if file.log does not exist, the output will be the following:
You can have more than one function call in your Look at the following code. Here, you first call the
If the file does not exist, running this code on a Windows machine will output the following:
Inside the
Here are the key takeaways:
The else ClauseIn Python, using the Look at the following example:
If you were to run this code on a Linux system, the output would be the following:
Because the program did not run into any exceptions, the You can also
If you were to execute this code on a Linux machine, you would get the following result:
From the output, you can see that the Cleaning Up After Using finallyImagine that you always had to implement some sort of action to clean up after executing your code. Python enables you to do so using the Have a look at the following example:
In the previous code, everything in the
Summing UpAfter seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python. In this article, you saw the following options:
Hopefully, this article helped you understand the basic tools that Python has to offer when dealing with exceptions. Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Raising and Handling Python Exceptions Apa itu Exception pada python?Assertions Exception adalah sebuah peristiwa, yang terjadi selama pelaksanaan program yang mengganggu aliran normal instruksi program. Secara umum, ketika skrip Python menemukan situasi yang tidak dapat diatasi, hal itu menimbulkan pengecualian. Exception adalah objek Python yang mewakili kesalahan.
Apa itu try pada python?Blok try memungkinkan untuk menguji blok kode terhadap kesalahan.
|