Okay so the probabilities are you already find out 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 sudden occurs along with your code. Simply suppose that you simply writing an enormous program which browses the web and accesses internet pages, this system works superb in your aspect and also you take into account packaging and distributing it. However one thing sudden happens on the computer of the person who downloaded your bundle 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, knowledge, timeout)
File "C:Python27liburllib2.py", line 404, in open
response = self._open(req, knowledge)
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
increase URLError(err)
So to be able to save your self from this you need to use Exception Dealing with
. There are three key phrases out there inside python which aid you to deal with exceptions. They’re often known as try-except
clause. Simply watch the beneath code.
>>> strive:
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 strive besides clause. Lets get considerably deeper into it. We wrap our predominant code within the strive clause. After that we wrap some code in besides clause which will get executed if an exception happens within the code wrapped in strive 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.
>>> strive:
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 possibly can see the lastly clause received 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 received 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 in the long run 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:
>>> strive:
print ali
besides KeyboardInterrupt:
print "'E's pining!"
besides :
print "That is going to deal with each kind of exception"
lastly:
print "okay the top"
That is going to deal with each kind of exception
okay the top
Okay i assume 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 at this time’s publish. Do share your views about todays publish within the feedback beneath.