Skip to main content

Python 3 Part 9 - Exception Handing

 Python 3 Part 9 - Exception Handing

Table of content

  1. Exceptions
  2. Handling exceptions
  3. Finally block 
  4. How to raise exceptions

Exceptions

Even if the code is logically correct but sometimes It may cause an unwanted behavior while executing the code is termed as Exceptions.

Python has a list of built-in exception list, that can found here : https://docs.python.org/3/library/exceptions.html#bltin-exceptions

Example of an exception :
                    >>> num = 1/0
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    ZeroDivisionError: division by zero
                    >>>

Handling Exception

Syntax for exception handling.

                    try:
                        statements
                    except <Exception_Name1>:
                        statements
                    except <Exception_Name2>:
                        statements


More generic way to handles any exception which occurs in try block. 

                    try:
                        statements
                    except:                    
                        statements

Finally block

Finally block executed irrespective of exception occurred or not. Syntax for finally block.

                    try:
                        statements
                    except:                     
                        statements
                    finally:
                        statements

Example 1 :
                    try:
                        num = 1/0
                    except ZeroDivisionError:
                        print("Zero Division Error occurred")
                    finally:
                        print("In finally-block")
 

output
                    Zero Division Error occurred
                    In finally-block

Example 2 :
                    try:
                        num = 1/0
                    except :
                        print("Zero Division Error occurred")
                    finally:
                        print("In finally-block")

output
                    Zero Division Error occurred
                    In finally-block
 

We can handling multiple exceptions in a single except block :

                    except (RuntimeError, TypeError, NameError):
                        pass

How to raise Exceptions?

We can throw an exception using the raise keyword. Below example raises generic Exception and NameError.
                    >>> raise Exception("Exception Occurred")
                    Traceback (most recent call last):
                      File "<pyshell#16>", line 1, in <module>
                        raise Exception("Exception Occurred")
                    Exception: Exception Occurred
                    >>> raise NameError("Name Error Exception")
                    Traceback (most recent call last):
                      File "<pyshell#17>", line 1, in <module>
                       raise NameError("Name Error Exception")
                    NameError: Name Error Exception

                    >>>

Thank you folks. If you like this Python 3 post, please do checkout my other post on  Django with Python 3 series

Most viewed

Ruby on rails part 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl