Predefined Exception handling




Hello folks....!!!
Today let us look at the concept of exception handling in python.

Exceptions 

The exceptions are nothing but an event that disrupts the normal flow of the program. When the python script we wrote encounters a situation which it cannot handle anymore, it raises an exception. It is the job of the programmer to handle exceptions effectively.

Difference between errors and exceptions

    Errors (also known as syntax errors) are caused due to the violation of syntax rules or missing the required element in the code snippet. In general these are reported by the Python interpreter. For example:


This is a typical syntax error. then colon ':' is missing after the condition. Similarly if you don't put parenthesis in print for python 3, you will get an error. Some of the errors are :
  • list index out of range
  • module not found error
  • key error-when a key is not found in dictionary
  • type error- data type violation
  • value error- when user gives input of inappropriate type.

  Exceptions are the ones which are raised when the statements are syntactically correct but an error arises when trying to execute it. Such things should be handled by the programmer. These occur in runtime. For example:
  • division by zero
  • variable_name not defined 
  • cannot convert one object type to another implicitly
To handle these exceptions one can use try-except blocks. This is just like if-else. When there are no exceptions, (if) try block will be executed but no condition is checked to enter into the try block. Otherwise except block statements will be executed.

try block

  • There is no condition required to be satisfied to enter into the try block.
  • The code in try block is executed until an exception occurs.
  • If no error occurs, the except block will be skipped. 

except block

  • The code to be executed when an error is encountered at the runtime is placed in this block.
  • This portion will be skipped if no error is encountered in the try part.
  • Even multiple except blocks can be defined for a single try block.
  • You can even customize the message displayed for syntax errors.
For example :
normal ValueError :


customized handling ValueError : You expect a integer value, but the user inputs a string value. 


Division by zero error 
without customization


with customized error message using try-expect block. When you give correct inputs :


When you don't give correct inputs :


Thus look at the syntax errors generated in various situation note the name (eg : ZeroDivisionError, ValueError) and write your customized message in except block. Note that you should not change the spelling, you should write as it is. 

You can even handle multiple exceptions in same program. For a single try block you can have any number of except blocks. Look at the code below.


You can have if conditions is try block and based on that you can call the exceptions appropriately.

Now can I customize my own exception instead of just customizing the error message ???
The answer is yes...!!!

I will post how to write user defined exceptions in tomorrow's post.


Next Topic👉

Comments