Raising an exception
For error handling, you may wish Python to throw a custom exception. This may be accomplished by testing a condition and raising an exception if it is true. The user or calling program is notified when an exception is triggered.
To manually throw a Python exception, use the "raise" keyword. You may also include a message to explain why the exception occurred.
Example :
def fun(a,b,c,d):
if b==0:
raise Exception("Can't divide, the denomination is Zero")
a = a//b
a += c
a+= b
return a
print(fun(1,0,3,4))
You can also try this code with Online Python Compiler
Run Code
Output:
Traceback (most recent call last):
File "d:\Course\Codeforces\New Codeforces\temp.py", line 9, in <module>
print(fun(1,0,3,4))
File "d:\Course\Codeforces\New Codeforces\temp.py", line 3, in fun
raise Exception("Can't divide, the denomination is Zero")
You can also try this code with Online Python Compiler
Run Code
In the above piece of code, we can see that a is getting divided by b. If b is zero, then it is a logical fault as a number cannot be divided by zero. Hence, we can manually raise an exception if such a case occurs.
Traceback
When Python encounters an error, it generates the traceback, which is a mine of error information. The error message, the line number of the line that created the error, and the sequence of function calls that lead to the error are all included in the traceback.
Example:
def fun2():
raise Exception('An exception occured')
def fun1():
fun2()
fun1()
You can also try this code with Online Python Compiler
Run Code
Output:
Traceback (most recent call last):
File "d:\Course\Codeforces\New Codeforces\temp.py", line 7, in <module>
fun1()
File "d:\Course\Codeforces\New Codeforces\temp.py", line 5, in fun1
fun2()
File "d:\Course\Codeforces\New Codeforces\temp.py", line 2, in fun2
raise Exception('An exception occured')
Exception: An exception occured
You can also try this code with Online Python Compiler
Run Code
You can try it on online python compiler.
When you run the above piece of code, you will see an output similar to the above.
We can see that the error occurred on line 2 inside the fun2() function. This fun2() was called by fun1() in line number 5. The fun1() inturn was called by line 7. The traceback makes it easier to find where did the error occur and what was the error occurred, and the call stack, i.e., which functions were called before it.
Avoid raising generic exceptions.
When raising exceptions, avoid raising generic Exceptions. Instead, try to be as specific as you can while raising exceptions. For example, use
raise ValueError(‘An error message’)
You can also try this code with Online Python Compiler
Run Code
when you are sure that ValueError Exception will occur instead of
raise Exception(‘An error message’)
You can also try this code with Online Python Compiler
Run Code
You can also read about the Multilevel Inheritance in Python.
Frequently Asked Questions
-
What is the difference between exceptions and errors?
The problems in the program due to which the program is not able to complete its execution are called errors. In contrast, exceptions are raised due to some internal event that obstructs the normal flow of the execution.
-
Is there any way to write the traceback information to a text file?
Yes, traceback.format_exc() function in the traceback module can be used to write the traceback information to a text file. For this, you have to import the traceback module into the program.
-
Which class is the base class of all exceptions?
The class BaseException is the base class of all the exceptions. However, user-defined classes cannot inherit from this class. They need to inherit from the Exception class.
Key Takeaways
Congratulations on making it this far.
In this blog, we understood Python's type-conversion and typecasting.
If you want to become proficient with Python programming, I suggest you take the Coding Ninjas Python Course, which will teach Python basics with Data Structures and Algorithms.