Exceptions

Exceptions are events that modify a program's flow, either intentionally or due to errors. Some examples are trying to open a file that doesn't exist, or when the program reaches a marker, such as the completion of a loop. Exceptions, by definition, don't occur very often; hence, they are the exceptions to the rule and a special class has been created for them.

Exceptions are everywhere in Python. Virtually every module in the Python standard library uses them, and Python itself will raise them in a variety of different circumstances. Here are just a few examples:

  • Accessing a non-existent dictionary key will raise a KeyError exception
  • Searching a list for a non-existent value will raise a ValueError exception
  • Calling a non-existent method of a class will raise an AttributeError exception
  • Referencing a non-existent variable will raise a NameError exception
  • Mixing data types without coercion will raise a TypeError exception

One use of exceptions is to catch a fault and allow the program to continue working; this is the most common way to use exceptions. If the developer can anticipate possible errors, then the exception-catching code can be written to deal with them. This not only allows the program to continue working, but also hides the ugliness of broken software from the user. From a security perspective, it also helps make it more difficult to screenshot out what a program does if someone is looking for vulnerabilities to exploit.

When programming with the Python command-line interpreter, you generally don't need to worry about catching exceptions. Your program is usually short enough to not be hurt too much if an exception occurs. Plus, having the exception occur at the command-line is a quick and easy way to tell if your code logic has a problem. However, if the same error occurred in your real program, it would fail and stop working.

Exceptions can be created manually in the code by raising an exception. It operates exactly as a system-caused exception, except that the programmer is doing it on purpose for a number of reasons. One of the benefits of using exceptions is that, by their nature, they don't put any overhead on the code processing. Because exceptions aren't supposed to happen very often, they aren't processed until they occur.

Exceptions can be thought of as a special form of the if...else statements. You can realistically do the same thing with if blocks as you can with exceptions. However, as already mentioned, exceptions aren't processed until they occur; if blocks are processed all the time. Proper use of exceptions can help the performance of your program. The more infrequent the error might occur, the better off you are to use exceptions; using if blocks requires Python to always test extra conditions before continuing. Exceptions also make code management easier: if your programming logic is mixed in with error-handling if statements, it can be difficult to read, modify, and debug your program.

The following is a simple program that highlights most of the important features of exception processing:

1 num1 = input("Enter the first number: ") 
2 num2 = input("Enter the second number: ") 
3 try: 
4    num1 = float(num1) 
5    num2 = float(num2) 
6    result = num1/num2 
7 except ValueError: 
8    print ("Two numbers are required.") 
9 except ZeroDivisionError: 
10   print ("Zero can't be a denominator.") 
11 else: 
12   print ("{num1}/{num2}={result}".format(num1=num1, num2=num2, result=result)) 

This program simply takes two numbers from the user, divides them, and then shows the calculation and result to the user. If the user forgets to enter a number or attempts to divide by zero, the resultant errors are caught and the user is politely informed of the problem. Following screenshot shows the output of this program:

Exception example output

As demonstrated in the code, you can catch multiple exceptions within the same try block. You can also put an optional else statement at the end to denote the logic to perform if all goes well. Exceptions assume the "default" case to be true until an exception actually occurs. This speeds up program processing, compared to if...else statements that check each if line for a true or false condition.

One change you could make to this program is to simply put it all within the try block. The input() variables (which capture input from the user's keyboard) could be placed within the try block, replacing the num1 and num2 variables by forcing the user input to a float value, as shown here:

try: 
    numerator = float(input("Enter the numerator.")) 
    denominator = float(input("Enter the denominator.")) 

This way, you reduce the amount of logic that has to be written, processed, and tested. You still have the same exceptions; you're just simplifying the program.

If you want to ensure some actions take place at the end of the try block, you can add a finally block at the end. While try/except is used to catch and recover from errors, try/finally is used to ensure that some sort of action takes place, regardless of whether any exceptions actually occur, such as closing files, severing server connections, and so on.

When planning your project, it's better to include error-checking, such as exceptions, in your code as you program rather than as an afterthought. A special "category" of programming involves writing test cases to ensure that most possible errors are accounted for in the code, especially as the code changes or new versions are created. By planning ahead and putting exceptions and other error-checking code into your program at the outset, you ensure that problems are caught before they can cause problems. By updating your test cases as your program evolves, you ensure that version upgrades maintain compatibility and that a fix doesn't create an error condition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.98.208