Handling Exceptions.

Some simple examples for exception handling:

C#

Python:

x = 10
y = 20
try:
    if x > y:
        print "abcd"
    else:
#        dividing by 0
        print 2 / 0

except Exception as e:
    print "hit an exception : {0} : {1}".format(e, e.message)

#Output:
#hit an exception : integer division or modulo by zero : integer division or modulo by zero
###

In Python there are two keywords related to exceptions:

  • pass.
    • Think of this as same as continue.
  • raise
    • If we want to bubble up the exception, then ‘raise’ it . Else the exception is suppressed.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s