Exception handling enables you handle errors gracefully and do something meaningful about it. Like display a message to user if intended file not found. Python handles exception using try..except.. block.

Syntax:

1

2

3

4

5

try:

# write some code

# that might throw exception

except<ExceptionType>:

# Exception handler, alert the user

As you can see in try block you need to write code that might throw an exception. When exception occurs code in the try block is skipped. If there exist a matching exception type in except clause then it’s handler is executed.

Let’s take an example:

1

2

3

4

5

6

try:

f=open('somefile.txt','r')

print(f.read())

f.close()

exceptIOError:

print('file not found')

The above code work as follows:

1. First statement between try and except block are executed. 2. If no exception occurs then code under except clause will be skipped. 3. If file don’t exists then exception will be raised and the rest of the code in the try block will be skipped 4. When exceptions occurs, if the exception type matches exception name after except keyword, then the code in that except clause is executed.

Note: The above code is only capable of handling IOError exception. To handle other kind of exception you need to add more except clause.

A try statement can have more than once except clause, It can also have optional else and/or finally statement.

1

2

3

4

5

6

7

8

9

10

11

12

try:

<body>

except<ExceptionType1>:

<handler1>

except<ExceptionTypeN>:

<handlerN>

except:

<handlerExcept>

else:

<process_else>

finally:

<process_finally>

except clause is similar to elif . When exception occurs, it is checked to match the exception type in except clause. If match is found then handler for the matching case is executed. Also note that in last except clause ExceptionType is omitted. If exception does not match any exception type before the last except clause, then the handler for last except clause is executed.

Note: Statements under the else clause run only when no exception is raised.

Note: Statements in finally block will run every time no matter exception occurs or not.

Now let’s take an example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

try:

num1,num2=eval(input("Enter two numbers, separated by a comma : "))

result=num1/num2

print("Result is",result)

exceptZeroDivisionError:

print("Division by zero is error !!")

exceptSyntaxError:

print("Comma is missing. Enter numbers separated by comma like this 1, 2")

except:

print("Wrong input")

else:

print("No exceptions")

finally:

print("This will execute no matter what")

Note: The eval() function lets a python program run python code within itself, eval() expects a string argument.

Raising exceptions

To raise your exceptions from your own methods you need to use raise keyword like this

1

raiseExceptionClass("Your argument")

Let’s take an example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

defenterage(age):

ifage<0:

raiseValueError("Only positive integers are allowed")

ifage%2==0:

print("age is even")

else:

print("age is odd")

try:

num=int(input("Enter your age: "))

enterage(num)

exceptValueError:

print("Only positive integers are allowed")

except:

print("something is wrong")

Run the program and enter positive integer.

Expected Output:

1

2

Enter your age:12

age iseven

Again run the program and enter a negative number.

Expected Output:

1

2

Enter your age:-12

Only integers are allowed

Using Exception objects

Now you know how to handle exception, in this section we will learn how to access exception object in exception handler code. You can use the following code to assign exception object to a variable.

1

2

3

4

try:

# this code is expected to throw exception

exceptExceptionType asex:

# code to handle exception

As you can see you can store exception object in variable ex . Now you can use this object in exception handler code

1

2

3

4

5

try:

number=eval(input("Enter a number: "))

print("The number entered is",number)

exceptNameErrorasex:

print("Exception:",ex)

Run the program and enter a number.

Expected Output:

1

2

Enteranumber:34

The number entered is34

Again run the program and enter a string .

Expected Output:

1

2

Enteranumber:one

Exception:name'one'isnotdefined

Creating custom exception class

You can create a custom exception class by Extending BaseException class or subclass of BaseException .

As you can see from most of the exception classes in python extends from the BaseException class. You can derive you own exception class from BaseException class or from sublcass of BaseException like RuntimeError .

Create a new file called NegativeAgeException.py and write the following code.

1

2

3

4

classNegativeAgeException(RuntimeError):

def__init__(self,age):

super().__init__()

self.age=age

Above code creates a new exception class named NegativeAgeException , which consists of only constructor which call parent class constructor using super().__init__() and sets the age .

Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

thank you so much for this tutorial. I have been following from the beginning and have really enjoyed your clear explaination, great exapmles and the clean layout of your site. I ran the example exactly as it is in the “Raising exceptions” section, but no matter what my input is, only the ValueError is thrown. It seems that the last except “something is wrong” is never executed. For example, even if I put in a letter instead of a number, I still got the ValueError. do you know why it is happening?