Home » Python

except keyword with example in Python

Python except keyword: Here, we are going to learn about the except keyword with example.
Submitted by IncludeHelp, on April 14, 2019

Python except keyword

except is a keyword (case-sensitive) in python, it is used with try... except statement to handle the exception.

except keyword defines a block which executes if statements are written in try block raise an error.

Note: We can define multiple blocks with except keyword to handle the different types of expectations by mentioning the error/exception names.

Syntax of except keyword

    try:
        statement(s)-1
    except:
        statement(s)-2

While executing the statement(s)-1, if there is any exception raises, control jumps to except block and statement(s)-2 executes.

Syntax of except keyword with multiple except blocks

    try:
        statement(s)-1
    except Error_Name1:
        statement(s)-A
    except Error_Name2:
        statement(s)-B
    except Error_Name3:
        statement(s)-C
    ..
    except:
        statement(s)-default

While executing the statement(s)-1 if Error_Name1 generates, statements written in Except Error_Name1 (statements(s)-A) executes, and so on... If any error is not mentioned with except block then last except block without any error name executes.

Example:

    Input:
    a = 10
    b = 0

    try:
        # no error
        result = a%b
        print(result)
    
    except:
        print("There is an error")

    Output:
    There is an error

Python examples of except keyword

Example 1: Find modulus of two number and handle exception, if divisor is 0.

# python code to demonstrate example of 
# except keyword 

# Find modulus of two number and 
# handle exception, if divisor is 0

a = 10
b = 3

try:
    # no error
    result = a%b
    print(result)
    
    # assign 0 to b
    # an error will occur
    b = 0
    result = a%b
    print(result)
    
except:
    print("There is an error")

Output

1
There is an error

Example 2: Write an example to handle multiple errors.

# python code to demonstrate example of 
# except keyword 

# Write an example to handle multiple errors

a = 10
b = 3

try:
    # del b # uncomment this to test NameError
    # no error
    result = a%b
    print(result)
    
    # assign 0 to b
    # an error will occur
    b = 0
    result = a%b
    print(result)
    
except ZeroDivisionError:
    print("Can not divide by 0")
except NameError:
    print("A NameError in the code")
except:
    print("There is an error")

Output

1
Can not divide by 0





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.