PYTHON
AGENDA
Exception
Handling an exception
What is Exception?
 An exception is an event, which occurs during
the execution of a program, that disrupts the normal fl...
Exceptions Types
EXCEPTION NAME DESCRIPTION
ZeroDivisonError Raised when division or modulo by zero
takes place for all nu...
Handling an exception
If any suspicious code that may raise an exception
suspicious code in a try: block. After the try:...
• SYNTAX:
Here is simple syntax of try....except...else blocks:
try:
You do your operations here;
......................
e...
 A single try statement can have multiple except statements.
This is useful when the try block contains statements that m...
• Example
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print ...
• The except clause with multiple exceptions:
try:
You do your operations here;
......................
except(Exception1, ...
• The try-finally clause:
The finally block is a place to put any code that must
execute, whether the try-block raised an ...
• Raising an exceptions:
Raise an exceptions in several ways by using the raise
statement.
SYNTAX:
raise Exception [ args ...
• Example:
level = 1
if level < 2:
raise "Invalid level!", level
# The code below to this would not be executed
# if we ra...
• In order to catch an exception, an "except" clause must refer to
the same exception thrown either class object or simple...
Exception
Upcoming SlideShare
Loading in...5
×

Exception

51

Published on

Python, All about Exception handling in python

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
51
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Exception

  1. 1. PYTHON
  2. 2. AGENDA Exception Handling an exception
  3. 3. What is Exception?  An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.  In general, when a Python script encounters a situation that it can't cope with, it raises an exception..  When a Python script raises an exception, it must either handle the exception immediately otherwise it would terminate and come out.
  4. 4. Exceptions Types EXCEPTION NAME DESCRIPTION ZeroDivisonError Raised when division or modulo by zero takes place for all numeric types. ImportError Raised when an import statement fails. IOError Raised when an input/ output operation fails, the open() function when trying to open a file that does not exist. SyntaxError Raised when there is an error in Python syntax. IndentationError Raised when indentation is not specified properly.
  5. 5. Handling an exception If any suspicious code that may raise an exception suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
  6. 6. • SYNTAX: Here is simple syntax of try....except...else blocks: try: You do your operations here; ...................... except ExceptionI: If there is ExceptionI, then execute this block. except ExceptionII: If there is ExceptionII, then execute this block. ...................... else: If there is no exception then execute this block.
  7. 7.  A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.  Also provide a generic except clause, which handles any exception.  After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception.
  8. 8. • Example try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") except IOError: print "Error: can't find file or read data" else: print "Written content in the file successfully" fh.close() Output: Written content in the file successfully
  9. 9. • The except clause with multiple exceptions: try: You do your operations here; ...................... except(Exception1, Exception2,...ExceptionN): If there is any exception from the given exception list, then execute this block. ...................... else: If there is no exception then execute this block.
  10. 10. • The try-finally clause: The finally block is a place to put any code that must execute, whether the try-block raised an exception or not try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") finally: print "Error: can't find file or read data" If you do not have permission to open the file in writing mode, Output: Error: can't find file or read data
  11. 11. • Raising an exceptions: Raise an exceptions in several ways by using the raise statement. SYNTAX: raise Exception [ args ] Here, Exception is the type of exception (for example, NameError) and argument is a value for the exception argument. The argument is optional; if not supplied, the exception argument is None.
  12. 12. • Example: level = 1 if level < 2: raise "Invalid level!", level # The code below to this would not be executed # if we raise the exception
  13. 13. • In order to catch an exception, an "except" clause must refer to the same exception thrown either class object or simple string. For example, to capture above exception, we must write our except clause as follows: try: Business Logic here... except "Invalid level!": Exception handling here... else: Rest of the code here...
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×