Home Python Exception dealing with – Yasoob Khalid

Exception dealing with – Yasoob Khalid

1
Exception dealing with – Yasoob Khalid

[ad_1]

Okay so the probabilities are you already learn about exception dealing with in python however some new programmers don’t. How do you deal with exceptions in python?

First let me inform you what exceptions are. Exceptions are when one thing surprising occurs along with your code. Simply assume that you just writing an enormous program which browses the web and accesses net pages, this system works positive in your aspect and also you contemplate packaging and distributing it. However one thing surprising happens on the computer of the person who downloaded your package deal from the web and tried to run it. His web connection dropped. What’s going to occur?

>>> import urllib2
>>> urllib2.urlopen('http://www.google.com/').learn()

Traceback (most up-to-date name final):
  File "", line 1, in 
    urllib2.urlopen('http://www.google.com/').learn()
  File "C:Python27liburllib2.py", line 127, in urlopen
    return _opener.open(url, information, timeout)
  File "C:Python27liburllib2.py", line 404, in open
    response = self._open(req, information)
  File "C:Python27liburllib2.py", line 422, in _open
    '_open', req)
  File "C:Python27liburllib2.py", line 382, in _call_chain
    end result = func(*args)
  File "C:Python27liburllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:Python27liburllib2.py", line 1184, in do_open
    elevate URLError(err)

So with a purpose to save your self from this you need to use Exception Dealing with. There are three key phrases out there inside python which enable you to to deal with exceptions. They’re often known as try-except clause. Simply watch the beneath code.

>>> attempt:
	urllib2.urlopen('http://www.google.com/').learn()
besides urllib2.URLError as e:
	print "An error " 
	
An error
>>> 

Right here we’re doing the identical factor however utilizing the attempt besides clause. Lets get considerably deeper into it. We wrap our principal code within the attempt clause. After that we wrap some code in besides clause which will get executed if an exception happens within the code wrapped in attempt clause. However wait the place is the third key phrase? The third key phrase is lastly. It comes after the besides clause and will get executed even when no exception happens. Let me present you a similar instance however this time with the lastly clause as effectively.

>>> attempt:
	urllib2.urlopen('http://www.google.com/').learn()
besides urllib2.URLError as e:
	print "An error %s " %e
lastly:
	print "That is going to be printed even when no exception happens"
<code>
'Google(perform(){nwindow.google={kEI:"sdD2UbmEOMzBtAbyqYCgDQ",ge
</code>
------------------------trancuated------------------- 
That is going to be printed even when no exception happens

In order you’ll be able to see the lastly clause obtained executed even when there was no exception. Okay now i’m going to inform you somewhat secret which most of us don’t know and even i obtained to comprehend it simply few days in the past. Whereas utilizing the besides clause if we need to deal with any exception that we’re not conscious of then what ought to we do? Right here on this state of affairs we are able to put extra besides clauses for every exception which is more likely to happen and ultimately we are able to put one other besides clause which caches all exceptions which weren’t more likely to happen. Right here is an instance:

>>> attempt:
    print ali
besides KeyboardInterrupt:
    print "'E's pining!"
besides :
    print "That is going to deal with each sort of exception"
lastly:
    print "okay the tip"

That is going to deal with each sort of exception
okay the tip

Okay i suppose thats it for now. Nonetheless i simply gave you an intro of exception dealing with in python and if you wish to research additional then go to this hyperlink. I hope you preferred as we speak’s put up. Do share your views about todays put up within the feedback beneath.

Supply : http://docs.python.org/2/tutorial/errors.html

[ad_2]

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here