Featured Post

Keyword to create root window in python

What are error and exceptions in python


 

(9)ERRORS AND EXCEPTIONS:



A python program terminates as soon as it encounters an error the error could be a syntax error or exceptions.


1.Syntax error:

A syntax error occurs when there is an incorrect statement.

for example: 

= 5 print(a)

output = 

a = 5 print(a)

              ^

SyntaxError: invalid syntax


2.Type error: 

A type error occurs if an operation performed on an incorrect object type.

for example: 

= 5 + '10'

output = a = 5 + '10'

TypeError: unsupported operand type(s) for +: 'int' and 'str'


3.Module not found error:   

for example: 

import somemodule

output = 

import somemodule

ModuleNotFoundError: No module named 'somemodule'


4.Name error: 

A name error occurs when a name used is not defined.

for example:

= 5 

= c

output = b = c

NameError: name 'c' is not defined


5.File not found error:

A file not found error occurs when the code detects no such file you said to detect.

for example: 

= open('somefile.txt')

output = f = open('somefile.txt')

FileNotFoundError: [Errno 2] No such file or directory: 'somefile.txt'


6.Value error:

for example: 

= [123]

a.remove(4)

print(a)

output =  a.remove(4)

ValueError: list.remove(x): x not in list


7.Index error: 

for example: 

= [1,2,3]

a[4]

print(a)

output = a[4]

IndexError: list index out of range


8.key error:

for example:

my_dict = {'name''Max'}

my_dict['age']

output =  my_dict['age']

KeyError: 'age'


9.Exceptions:

Raise an exeption by putting 

for example : 

= -1

if x < 0:
    raise Exception('x should be positive')   

output = raise Exception('x should be positive')

Exception: x should be positive

H

10.Assertion error:

for example:

= -1

assert (x>=0), 'x is not posintive'

output = assert (x>=0), 'x is not posintive'

AssertionError: x is not posintive


11.Try:

The ''try' function try's to run the code, and while running the code if there is an error it skip's it.It is helpful if you don't want to terminate you program while there is an error and want to continue.
If you want to run another code if the code in the try functions goes wrong you can use the 'except' function.

for example:

try:
    a = 5 / 0

except:
    print('an  error happend')

output = an error happened


To print a specific error.

for example: 

try:
    a = 5 / 0

except Exception as e:
    print(e)

output = division by zero 


To catch specific type of error type the type of error.

for example:

try:
    a = 5 / 1
    b = a + '10'

except ZeroDivisionError as e: 
    print(e)

except TypeError as e:
    print(e)

output = unsupported oprand type(s) for +: 'float and 'str'


If no error is there.

for example:

try:

   a = 5 / 1

   b = a + 10

except ZeroDivisionError as e: 

    print(e)

except TypeError as e:

    print(e)

else:
    print('evevrything is fine')

output = everything is fine


To do a task at the end of program no matter what happens use 'finally'.

for example:

try:
    a = 5 / 1
    b = a + 10

except ZeroDivisionError as e: 
    print(e)

except TypeError as e:
    print(e)

else:
    print('everything is fine')

finally:
    print('cleaning up...')

output = everything is fine

cleaning up... 


try:

try:
   a = 5 / 0
   b = a + 10

except ZeroDivisionError as e: 
    print(e)

except TypeError as e:
    print(e)

else:
    print('everything is fine')

finally:
    print('cleaning up...')

output = division by zero

cleaning up...


Making a custom error

for example:

class ValueTooHighError(Exception):
    pass

def test_value(x):

    if x >100:
        raise ValueTooHighError('value is too high')

test_value(200)

output  =  raise ValueTooHighError('value is too high')

__main__.ValueTooHighError: value is too high

Comments