Lỗi the exception unknown software exception trong audition năm 2024

In software development, different types of errors can occur. They could be syntax errors, logical errors, or runtime errors.

Syntax errors most probably occur during the initial development phase and are a result of incorrect syntax. Syntax errors can be caught easily when the program is compiled for execution.

Logical errors, on the other hand, are a result of improper logical implementation. An example would be a program accessing an unsorted list assuming it to be sorted. Logical errors are the most difficult ones to track.

Runtime errors are the most interesting errors which occur, if we don't consider all the corner cases. An example would be trying to access a non-existent file.

In this tutorial, we'll learn how to handle errors in Python and how to log the errors for a better understanding of what went wrong in the application.

Handling Exceptions in Python

Let's start with a simple program to add two numbers in Python. Our program takes in two parameters as input and prints the sum. Here is a Python program to add two numbers:

1

def addNumbers(a, b): 2

print a + b  
3

4

addNumbers(5, 10)

Try running the above Python program, and you should have the sum printed.

2

04

While writing the above program, we didn't really consider the fact that anything can go wrong. What if one of the parameters passed is not a number?

We haven't handled that case, hence our program would crash with the following error message:

1

Traceback (most recent call last): 2

def addNumbers(a, b): 1

3

def addNumbers(a, b): 3

4

def addNumbers(a, b): 5

def addNumbers(a, b): 6

def addNumbers(a, b): 7

def addNumbers(a, b): 8

def addNumbers(a, b): 9

We can handle the above issue by checking if the parameters passed are integers. But that won't solve the issue. What if the code breaks down due to some other reason and causes the program to crash? Working with a program which crashes on being encountered with an error is not a good sight. Even if an unknown error is encountered, the code should be robust enough to handle the crash gracefully and let the user know that something is wrong.

Handling Exceptions Using

2

05 and

2

06

In Python, we use the

2

05 and

2

06 statements to handle exceptions. Whenever the code breaks down, an exception is thrown without crashing the program. Let's modify the add number program to include the

2

05 and

2

06 statements.

1

def addNumbers(a, b): 2

2

3

3

2

5

4

2

7

def addNumbers(a, b): 6

2

9

def addNumbers(a, b): 8

print a + b  
1
print a + b  
2
print a + b  
3

Python would process all code inside the

2

05 and

2

06 statement. When it encounters an error, the control is passed to the

2

06 block, skipping the code in between.

As seen in the above code, we have moved our code inside a

2

05 and

2

06 statement. Try running the program and it should throw an error message instead of crashing the program. The reason for the exception is also returned as an exception message.

The above method handles unexpected exceptions. Let's have a look at how to handle an expected exception. Assume that we are trying to read a particular file using our Python program, but the file doesn't exist. In this case, we'll handle the exception and let the user know that the file doesn't exist when it happens. Have a look at the file reading code:

1

print a + b  
5

2

2

3

3

print a + b  
9

4

3

1

def addNumbers(a, b): 6

3

3

def addNumbers(a, b): 8

3

5

print a + b  
2

3

7

3

8

3

9

In the above code, we have handled the file reading inside an

2

16 exception handler. If the code breaks down because the

2

17 is unavailable, the error would be handled inside the

2

16 handler. Similar to the

2

16 exceptions, there are a lot more standard exceptions like

2

20,

2

21, and

2

22, to name a few.

Multiple Exceptions

We can handle multiple exceptions at a time by clubbing the standard exceptions as shown:

1

print a + b  
5

2

3

3

5

4

7

def addNumbers(a, b): 6

9

def addNumbers(a, b): 8

3

9

The above code would raise both the

2

16 and

2

24 exceptions when the program is executed.

2

25 Clause

Assume that we are using certain resources in our Python program. During the execution of the program, it encountered an error and only got executed halfway. In this case, the resource would be unnecessarily held up. We can clean up such resources using the

2

25 clause. Take a look at the below code:

1

print a + b  
5

2

4

5

3

2

3

4

4

9

def addNumbers(a, b): 6

addNumbers(5, 10)

1

def addNumbers(a, b): 8

addNumbers(5, 10)

3

print a + b  
2

addNumbers(5, 10)

5

3

8

3

9

If, during the execution of the above code, an exception is raised while reading the file, the

2

27 would be closed in the

2

25 block.

User-Defined Exceptions

So far, we have dealt with exceptions provided by Python, but what if you want to define your own custom exceptions? To create user-defined exceptions, you will need to create a class that inherits from the built-in

2

29 class. An advantage of creating user-defined exceptions is that they will make sense in our programs. For example, suppose you had a program that ensures that the discounted price of an item is not more than the sale price. Let's create a custom exception for this type of error.

1

addNumbers(5, 10)

9

2

1

1

Next, add the exception as follows:

1

1

3

2

1

5

3

1

7

4

1

9

def addNumbers(a, b): 6

Traceback (most recent call last): 1

In the code above, the

2

30 statement forces the

2

31 exception to occur.

Now, if you call the function with values where the

2

32 is greater than the price, you will get an error, as shown below.

1

Traceback (most recent call last): 3

2

Traceback (most recent call last): 5

3

Traceback (most recent call last): 7

4

Traceback (most recent call last): 9

def addNumbers(a, b): 6

def addNumbers(a, b): 01

def addNumbers(a, b): 8

def addNumbers(a, b): 03

The error above does not provide a descriptive message; let's customize it to give a detailed message of what the error means.

1

addNumbers(5, 10)

9

2

def addNumbers(a, b): 07

3

def addNumbers(a, b): 09

4

def addNumbers(a, b): 11

def addNumbers(a, b): 6

print a + b  
1

def addNumbers(a, b): 8

def addNumbers(a, b): 15

print a + b  
2

def addNumbers(a, b): 17

Now, let's apply the error and call our function.

1

1

3

2

1

5

3

def addNumbers(a, b): 23

4

1

9

def addNumbers(a, b): 6

Traceback (most recent call last): 1

def addNumbers(a, b): 8

def addNumbers(a, b): 29

print a + b  
2

def addNumbers(a, b): 31

Now, if you call the function, you will get the following error:

1

def addNumbers(a, b): 33

2

Traceback (most recent call last): 3

3

def addNumbers(a, b): 37

4

Traceback (most recent call last): 7

def addNumbers(a, b): 6

def addNumbers(a, b): 41

def addNumbers(a, b): 8

def addNumbers(a, b): 43

print a + b  
2

def addNumbers(a, b): 45

Logging in Python

When something goes wrong in an application, it becomes easier to debug if we know the source of the error. When an exception is raised, we can log the required information to track down the issue. Python provides a simple and powerful logging library. Let's have a look at how to use logging in Python.

1

print a + b  
5

2

def addNumbers(a, b): 49

3

4

5

4

2

3

def addNumbers(a, b): 6

def addNumbers(a, b): 55

def addNumbers(a, b): 8

4

9

print a + b  
2

7

3

8

addNumbers(5, 10)

1

def addNumbers(a, b): 62

addNumbers(5, 10)

3

def addNumbers(a, b): 64

addNumbers(5, 10)

5

def addNumbers(a, b): 66

def addNumbers(a, b): 67

As seen in the above code, first we need to import the logging Python library and then initialize the logger with the log file name and logging level. There are five logging levels:

2

33,

2

34,

2

35,

2

36, and

2

37. Here we have set the logging level to

2

34, so any message that has the level

2

34 will be logged.

Getting the Stack Trace

In the above code we had a single program file, so it was easier to figure out where the error had occurred. But what do we do when multiple program files are involved? In such a case, getting the stack trace of the error helps in finding the source of the error. The stack trace of the exception can be logged as shown:

1

def addNumbers(a, b): 69

2

3

def addNumbers(a, b): 73

4

def addNumbers(a, b): 75

def addNumbers(a, b): 6

def addNumbers(a, b): 8

print a + b  
5
print a + b  
2

def addNumbers(a, b): 81

3

8

2

3

def addNumbers(a, b): 62

4

9

def addNumbers(a, b): 64

addNumbers(5, 10)

1

def addNumbers(a, b): 66

addNumbers(5, 10)

3

def addNumbers(a, b): 90

addNumbers(5, 10)

5

def addNumbers(a, b): 92

def addNumbers(a, b): 93

If you try to run the above program, on raising an exception the following error would be logged in the log file:

1

def addNumbers(a, b): 95

2

Traceback (most recent call last): 3

def addNumbers(a, b): 99

4

2

01

def addNumbers(a, b): 6

2

03

Wrapping It Up

In this tutorial, we saw how to get started with handling errors in Python and using the logging module to log errors. We saw the usage of

2

05,

2

06, and

2

25 statements, which are quite useful when dealing with error handling in Python. For more detailed information, I would recommend reading the official documentation on logging. Also have a look at the documentation for handling exceptions in Python.

This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.