When you create an application and try to execute its code, you have to face errors many times. Mostly errors either do not allow the program to execute (compile-time) or prevent the program from executing (run-time).

You may encounter two types of errors while executing Python code.

  • Syntax Errors / Parsing Errors
  • Exceptions/ Runtime Errors

We Provide Best Python training in Chandigarh. If you are Searching Python Training institute in Chandigarh then CBitss is Right Place. For More Details Call Us – +91 99 88 74 1983

Syntax Errors or Parsing Errors

Syntax errors are basically caused by writing the wrong syntax. You can call them grammatical errors. For example, if you do not put a colon (:) while defining the function and try to execute the code, then parser generates syntax-error.

Syntax errors are identified by the parser while parse or compile. These errors are removed by removing the wrong syntax and applying the correct syntax.

Introduction to Python Errors & Exceptions
Introduction to Python Errors & Exceptions

The parser message shows when a syntax error occurs. In that message, the parser displays the complete line in which the error has occurred. Apart from this, the parser also shows the place through the up arrow (^) in the line where the error is detected.

Most of the error comes after the up arrow shown by the parser. Apart from this, file name and line number are also shown by the parser so that you can easily find the code where the error has occurred.

Exceptions or Run Time Errors

Exceptions are errors that occur during code execution. These are also called run time errors. Exceptions are very fatal for any application. Because it stops the program while running. This deteriorates the user experience and can cause important work to get stuck in the middle.

The parser displays a message when an exception is generated. This message displays the type of parser exception and the location from which the exception may have been generated.

Exception type is basically the name of an exception which briefly describes which exception is generated. The location of the exception is shown as a stack traceback so that you can track the exact location where the exception is generated.

Handling Exceptions in Python

Like Syntax error, you cannot fix an exception and prevent it from being generated. Yes, but you can handle exceptions that are generated.

Python provides you with the mechanism to handle exceptions. This mechanism provides you with the ability to handle the situation by working in an organized way instead of failing the application badly when an exception is generated.

The exception handling mechanism in Python works by some keywords.

  • try 
  • except
  • else
  • raise
  • finally

Different blocks are defined by all these keywords which execute in different situations.

try Block 

The try block is defined by the try keyword. The try block holds code that could possibly generate an exception.

try:       #statements that may generate exception

If no exception is generated in the try block then no statement of except blocks is executed and all except blocks are discarded.

If an exception is generated in the try block and defines the except block associated with that exception, then the statements of that except block are executed.

If no except block is found for the exception, the application stops and the exception message is displayed by the parser.

The execution proceeds after the exception block is handled.

Read More AT – Python Language Features And Usage

except Block

The except block is defined by the except keyword immediately after the try block. When defining except block, exception type is defined after except keyword.

If an exception of type defined in except block occurs in the try block defined before the except block, then all the statements written in the except block are executed.

Multiple except blocks can be defined after the try block. For any type of exception you want to handle, define except block separately. The except block corresponding to the type of exception generated in the try block is executed and the others are skipped.

try:    #statements that may generate exceptionexcept <exception-Type>:    #statements to be executed when <exception-Type> occurs in above try blockexcept <exception-Type>:    #statements to be executed when <exception-Type> occurs in above try block

Multiple exceptions can also be defined by an except block. For this, you write the names of those exceptions separated by commas in brackets.

except(ValueError, NameError, TypeError)

Exception Argument

Each exception has a value associated with it which is called the exception argument. It is defined after the name of the exception in the except block.

except valueError as exObj:    print(exObj)

This provides detail about the argument exception. The details of the exception can also be seen by printing it.

else Block 

Else block is defined after except blocks. In this block, you would write the statements that you want to execute in case of no exception being raised by the try block.

else:    #statements to be executed if the try block does not generate an exception

Else block helps in avoiding situations when except blocks try to handle exceptions that have not been generated in the try block.

raise Statement 

If you think that a statement or condition can generate an exception, then for that condition you can manually generate an exception. The raise keyword is used for this.

raise TypeError(‘Type Exception Occurred!’)

To raise an exception, you type the name of the exception after the raise keyword, and then pass the message you want to display in brackets as an argument.

Python Built-in Exceptions

Built-in exceptions are exceptions that have already been defined in python. Some examples of built-in exception such as TypeError, ValueError and NameError etc.

For information on all the built-in applications available in Python, visit the Python Built-in Exceptions page.

User Defined Exceptions

Python also provides you with the ability to define your own custom exceptions. These are called user defined exceptions.

To define a custom exception, you define a class. In order to treat this class as a custom application it is necessary to derive it from python’s Exception class.

class myCustomException:Exception    //define exception here…

The Exception class in Python is the base class of all built in and user defined exceptions.

Related Article –