Hello readers, welcome back to know Python Exceptions and its real-time use in the software applications. One of the most powerful features of the language is exception handling. Due to how dynamic Python can be, it is very common for the interpreter to encounter multiple sources of error. With Python, it is not only possible to identify and track errors but to take corrective and preventive actions.
Exceptions are categorized into checked exceptions and unchecked exceptions. checked exceptions are the exceptions where we can identify the error and handle during the execution process whereas unchecked exceptions are the exceptions which are handled by the language and Python is richly featured in Exception Handling.
Python Syntax Error:
Syntactic errors are the most common when writing code. Syntactic errors occur when the interpreter finds an expression that is not well-formed (breaks the syntax rules).
The exceptions are logical errors that stop the execution of the program even when the syntax of the code is correct.
>>> print hello SyntaxError: Missing parentheses in call to 'print' #Exception: >>> print(Hello) Traceback (most recent call last): File "<pyshell#0>", line 1, in print(Hello) NameError: name 'Hello' is not defined
Analysis of Exceptions:
Next, some types of exceptions will be analyzed based on a code likely to incur in several of them. Even though the function is very simple and contains syntactically correct code, it can generate various errors, which will be analyzed.
>>> def exception_probable(num): number = float(num) print("The Square root of %f is %f" %(number, number**0.5)) >>> exception_probable(3.2) The Square root of 3.200000 is 1.788854 #incorrect data >>> exception_probable(Hello) Traceback (most recent call last): File "<pyshell#3>", line 1, in exception_probable(Hello) NameError: name 'Hello' is not defined
From the above code snippet, entering a positive real number, the function will run without problems. The only thing that must be done for an exception to occur is to enter incorrect data as shown in the above example. Because the float () function is used, the interpreter will try to convert the object with the name number into an object of type str. The error occurs because the interpreter identifies a name which is not defined in the namespace, so the execution of the script is stopped and a message is displayed as shown in the above code.
Anatomy of an error message:
The Python error message is divided into two parts. The error route and the description of the error.
Error Route tracking:
Because Python is highly modular, an error can be triggered at one point, but occur at another. When an error occurs, Python displays the route (traceback) and this route can involve several modules. The route goes in descending order from the point at which the error was triggered, going through several intermediate points -if there are any- to the point at which the error occurred. In the previous example Exceptions block, the error was triggered on #line 1 of the window that called the function. The interpreter even displays the content of the line.
Description of the Error:
The description of the error consists of a single line.
NameError : name ‘Hello’ is not defined
The line, in turn, consists of two parts:
- The type of error in question.
- A deep explanation of what happened:
the name ‘Hello’ is not defined
Possible types of exceptions:
Here are some of the exceptions that could arise when running the below script.
In the below script, the interpreter did not find a match in the namespace that corresponds to Hello and the exception is generated before invoking the function.
In the below script, the interpreter did not find a type of value for a given function, so the exception has generated after the execution. In this case, the error occurs on line 1 of exception_probable(), since the content of the object of type str does not correspond to a number.
In this case, the interpreter invokes exception_probable() and the error is generated in line 2 of the function since a number of type complex cannot be converted into a type of float.
In this exception_probable(-1), the error is triggered in exception_probable(), but now in line 3, because the square root of -1 results in an object of type complex and the function print() has the instruction to display it as a Float type object.
In this case, the exception is triggered before invoking the function, because yield is a reserved Python word.
>>> def exception_probable(num): number = float(num) print("The Square root of %f is %f" %(number, number**0.5)) # Defining Name Error >>> exception_probable(Hello) Traceback (most recent call last): File "<pyshell#7>", line 1, in exception_probable(Hello) NameError: name 'Hello' is not defined # Defining ValueError >>> exception_probable('Hello') Traceback (most recent call last): File "<pyshell#8>", line 1, in exception_probable('Hello') File "<pyshell#5>", line 2, in exception_probable number = float(num) ValueError: could not convert string to float: 'Hello' # Defining TypeError >>> exception_probable(13+1j) Traceback (most recent call last): File "<pyshell#9>", line 1, in exception_probable(13+1j) File "<pyshell#5>", line 2, in exception_probable number = float(num) TypeError: can't convert complex to float >>>exception_probable(-1) Traceback (most recent call last): File "<pyshell#10>", line 1, in exception_probable(-1) File "<pyshell#5>", line 3, in exception_probable print("The Square root of %f is %f" %(number, number**0.5)) TypeError: can't convert complex to float # Defining Syntax Error >>> exception_probable(yield) SyntaxError: invalid syntax
Real-Time Exceptions and their occurrence:
Few are mentioned in below list and python has a robust exception handling feature.
- Exception not only occur in the program or application but also occurs at MMU, CPU exceptions
- MMU exceptions are the hardware exceptions and CPU exceptions are the operating system exceptions, where we will run multi processes at a time.
- Exceptions occur when filling the web forms and submit. Here we need to check parameters before form submit. We simplify the process using exception handling
- Database connection’s exceptions, these are the main areas to handle correctly because data plays a crucial role in all times
- Internet communication or connection establishment, this comes into picture when we make net banking transactions or other banking services. Here we need to handle few connectivity exceptions.
- Sometimes operating system also uses exception handling to prevent deadlocks and data recovery from crashes.
There are few other types of exception built-in and explained with basic examples in next article.
If you like please share and comment