Python for Ethical Hackers
Mohammad reza Kamalifard
Python Language Essentials
Part 7 :
Exception Handling
Exceptions
Simply put exceptions are error condtions which disrupt the normal
flow of the program
Python allows for a simple and elegant way to handle exceptions
>>> 0/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
Python Standard Exceptions
http://www.tutorialspoint.com/python/standard_exceptions.htm
Exception Handling
>>> try:
... print 0/0
... except:
... print 'Exception Happened'
...
Exception Happened
>>>
>>> try:
... print 100/10
... except:
... print 'Exception'
...
10
>>>
else and finally
>>> try:
... print 10/2
... except:
... print 'Exception'
... else:
... print 'No Exception'
... finally:
... print 'Cleanup code always here'
...
5
No Exception
Cleanup code always here
>>>
else and finally
>>> try:
... print 10/0
... except:
... print 'Exception'
... else:
... print 'No Exception'
... finally:
... print 'Cleanup code always here'
...
Exception
Cleanup code always here
>>>
>>> try:
... 10/0
... except ZeroDivisionError:
... print 'Divided by Zero'
...
Divided by Zero
>>>
>>> try:
... 10/0
... except Exception as err:
... print err
...
integer division or modulo by zero
>>> try:
... print name
... except Exception as err:
... print err
...
name 'name' is not defined
>>>
This work is licensed under the Creative Commons
Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad reza Kamalifard.
All rights reserved.

اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی

  • 1.
    Python for EthicalHackers Mohammad reza Kamalifard
  • 2.
    Python Language Essentials Part7 : Exception Handling
  • 3.
    Exceptions Simply put exceptionsare error condtions which disrupt the normal flow of the program Python allows for a simple and elegant way to handle exceptions >>> 0/0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero Python Standard Exceptions http://www.tutorialspoint.com/python/standard_exceptions.htm
  • 4.
    Exception Handling >>> try: ...print 0/0 ... except: ... print 'Exception Happened' ... Exception Happened >>> >>> try: ... print 100/10 ... except: ... print 'Exception' ... 10 >>>
  • 5.
    else and finally >>>try: ... print 10/2 ... except: ... print 'Exception' ... else: ... print 'No Exception' ... finally: ... print 'Cleanup code always here' ... 5 No Exception Cleanup code always here >>>
  • 6.
    else and finally >>>try: ... print 10/0 ... except: ... print 'Exception' ... else: ... print 'No Exception' ... finally: ... print 'Cleanup code always here' ... Exception Cleanup code always here >>>
  • 7.
    >>> try: ... 10/0 ...except ZeroDivisionError: ... print 'Divided by Zero' ... Divided by Zero >>>
  • 8.
    >>> try: ... 10/0 ...except Exception as err: ... print err ... integer division or modulo by zero >>> try: ... print name ... except Exception as err: ... print err ... name 'name' is not defined >>>
  • 9.
    This work islicensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad reza Kamalifard. All rights reserved.