Python Exceptions

Python Exceptions

Python uses exceptions that are special objects to manage errors that occur during code execution.

When executing a program, if an error occurs that makes the compiler unsure about what to do next, then an exception object is created by the compiler.

If this exception’s object is “handled” then Python will continue running the code, this is called exception handling in Python. If you do not plan to handle the exception, the code will stop and give you a description of the error.

Raising Exceptions

Exceptions happen all of the time and as a developer, it is your task to handle them. But you can also create/raise your own exceptions like this –


Python Exceptions Raising Exceptions

Our code execution stopped when both of our values were equal to each other. Generally, it is used whenever we think that something can go wrong in our code, if that does happen, then we should know what went wrong in a descriptive manner.


Let us have an example of built-in exception objects in Python.


Python Exceptions ZeroDivisionError

It is quite obvious that Python cannot do this, so we get a ZeroDivisionError exception message, and this is an exception object. There are a lot of built-in exception objects in Python. You can check Python’s exception objects list here.

Exceptions are raised by programming languages to tell the user that there is something in the program that cannot be executed and must be changed.

Difference between Exception and Syntax Error

When the code that you write has a wrong statement or multiple wrong statements, then Python gives you a syntax error. Not only Python but pretty much every programming language will give a syntax error if the code is not written according to the language.

You have seen many syntax errors before, but still, let us have an example –


Python Exceptions Difference Exception Syntax Error

You get a syntax error this time because Python could not even get to the “Divide by Zero” condition let alone executing it.

Exception Handling

When a program is not executed it is because Python does not know what to do but what if we tell Python to do something when this kind of exception occurs? This can be done easily using the try and except block.


except ErrorName:
            error message

By using this method, you basically tell Python that, try running some code, if that does not work, print something on the screen or do something else.

You can also specify on which error you want to raise an exception.

Let us take an example of the ZeroDivisionError:


Python Exceptions Exception Handling

As you can see, instead of the usual traceback error, we got our string printed on the screen. When creating a program, you never want to show the user a traceback error because it might be too difficult for them to understand.

A traceback error can give hackers and malicious attackers valuable information that you do not want them to know.

Exception handling is most of the part of programming and should not be taken lightly.

Tutorials for all brains!