top of page
Writer's pictureEkta Aggarwal

Error exceptions with raise-try-except-else-finally

When there are errors in the codes then Python execution immediately stops. To avoid this we can define exceptions which does not yield to termination of Python code.


In this tutorial we shall learn the concepts:


raise

Let us create a function which calculates square of a number.

def square_func(x):
    return (x**0.5)

Our function returns square root of 25 correctly:

square_func(25)






But we know that square root of a negative number does not exist but Python is throwing junk value.

square_func(-25)



To tackle this we can raise the error using raise statement:

In the code below, if x is negative then a ValueError saying "Your number is not positive" gets raised, otherwise the function returns the square root of the number.

def square_func(x):
    if(x< 0):
        raise ValueError("Your number is not positive")
    else:
        return (x**0.5)
square_func(-25)

try - except







Let us reconsider our square_func as:

def square_func(x):
    return (x**0.5)

Let us see the output when we pass a string to our square_func

square_func('abcd')

Python returns an error and terminates the rest of the code. To tackle this we use the try-except statements.

In the following code chunk: we are telling Python to try returning square root of the number, if it encounters any error then Python throws the error: X must be a number.

def square_func(x):
    try:
        return (x**0.5)
    except:
        print("X must be a number")
square_func('abcd')





In the following code: Python tries returning the square root of the number using try statement.

If a TypeError is encountered then using except TypeError Python will print "Invalid type passed: X must be a number",

for other errors using except statement Python will print "Some other error encountered"

def square_func(x):
    try:
        return (x**0.5)
    except TypeError:
        print("Invalid type passed: X must be a number")
    except:
        print("Some other error encountered")

try - except- else











In the following code:

Python tries calculating the square root of the number using try statement.

If an error is encountered then using except Python will print "Invalid type passed: X must be a number",

If try statement can be executed successfully then Python will move to else statement and print the message that :No error was encountered and hence will return the square root.

def square_func(x):
    
    try:
        x**0.5
    except:
        print("Invalid type passed: X must be a number")
    else:
        print("No error was encountered")
        return(x**0.5)
square_func(25)

\


square_func('Hello')




try-except-else-finally















In the following code:

Python tries calculating the square root of the number using try statement.

If an error is encountered then using except Python will print "Invalid type passed: X must be a number",

If try statement can be executed successfully then Python will move to else statement and print the message that :"No error was encountered" and hence will return the square root.

Whether Python encounters any error or not, Python will always print the statement "Calculations are done!"



def square_func(x):
    try:
        x**0.5
    except :
        print("Invalid type passed: X must be a number")
    else:
        print("Square root calculated successfully")
        return(x**0.5)
    finally:
        print("Calculations are done!")
square_func('Hello')
square_func(25)
square_func(-25)


Comments


bottom of page