@vinaymavi

Exception Handling

Exception


    try:
    
    <code>
    
    except <exception_class>:
    
    finally:

    <code>

Exception

try:
    raise Exception("This is custom exception")
except Exception as e:
    print e
finally:
    print "Finally calling"

Exception

User defined exception

class MyError(Exception):
    def __init__(self,msg, *args, **kwargs):
        self.message = msg

    def __str__(self):
        return self.message

Exception

H.W

try:
    foo = open("foo.txt")
except IOError:
    print("error")
else:
    print(foo.read())
finally:
    print("finished")

Thank you.

Python-6

By Vinay Mavi

Python-6

Exception handling

  • 160