Python Built-in Exceptions Data Fusion Albert Esterline Source:

Slides:



Advertisements
Similar presentations
The State of the Python Union OSCON – August 3, 2005 Guido van Rossum Elemental Security, Inc.
Advertisements

Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Perkovic, Chapter 7 Functions revisited Python Namespaces
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
An Introduction to Python and Its Use in Bioinformatics
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Python Crash Course Programming Bachelors V1.0 dd Hour 5.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Functions Reading/writing files Catching exceptions
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions COMPSCI 105 SS 2015 Principles of Computer Science.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 4 Python Basics Part 3.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Python Exceptions and bug handling Peter Wad Sackett.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Rajkumar Jayachandran.  Classes for python are not much different than those of other languages  Not much new syntax or semantics  Python classes are.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
COMPSCI 107 Computer Science Fundamentals
C++ Exceptions.
Exceptions in Python Error Handling.
George Mason University
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python’s Modules by E. Esin GOKGOZ.
Exceptions and files Taken from notes by Dr. Neil Moore
Exception Handling Chapter 9.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Python’s Errors and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Exceptions.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
By Ryan Christen Errors and Exceptions.
Exception Handling COSC 1323.
Topics Introduction to File Input and Output
Python Exceptions and bug handling
UW CSE 190p Section 7/19, Summer 2012 Dun-Yu Hsiao.
Presentation transcript:

Python Built-in Exceptions Data Fusion Albert Esterline Source: For the exception hierarchy, see the bottom of the page, accessible directly at

The class hierarchy for built-in exceptions (See module exceptions ) BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StandardError | +-- BufferError | +-- ArithmeticError | | +-- FloatingPointError | | +-- OverflowError | | +-- ZeroDivisionError | +-- AssertionError | +-- AttributeError | +-- EnvironmentError | | +-- IOError | | +-- OSError | | +-- WindowsError (Windows) | | +-- VMSError (VMS)

+-- StandardError (cont.) | +-- EOFError | +-- ImportError | +-- LookupError | | +-- IndexError | | +-- KeyError | +-- MemoryError | +-- NameError | | +-- UnboundLocalError | +-- ReferenceError | +-- RuntimeError | | +-- NotImplementedError | +-- SyntaxError | | +-- IndentationError | | +-- TabError | +-- SystemError | +-- TypeError | +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError

+-- Exception (cont.) +-- Warning +-- DeprecationWarning +-- PendingDeprecationWarning +-- RuntimeWarning +-- SyntaxWarning +-- UserWarning +-- FutureWarning +-- ImportWarning +-- UnicodeWarning +-- BytesWarning

Exceptions used only as base classes for other exceptions BaseException Base class for all built-in exceptions Not meant to be directly inherited by user-defined classes Exception All built-in, non-system-exiting exceptions are derived from this class All user-defined exceptions should also be derived from this class StandardError Base class for all built-in exceptions except StopIteration, GeneratorExit, KeyboardInterrupt and SystemExit Derived from Exception ArithmeticError Base class for built-in exceptions raised for arithmetic errors

LookupError Base class for exceptions raised when a key or index used on a dictionary or sequence is invalid EnvironmentError Base class for exceptions that can occur outside the Python system: IOError, OSError When these exceptions are created with a 2-tuple,  1 st item is available on the instance’s errno attribute (assumed to be an error number), and  2 nd is available on the strerror attribute (usually the associated error message)  The tuple itself is also available on the args attribute When such an exception is instantiated with a 3-tuple,  1 st 2 items are available as above  3 rd is available on the filename attribute

Exceptions that are those actually raised AssertionError Raised when an assert statement fails. AttributeError Raised when an attribute reference or assignment fails When an object doesn’t support attribute references or attribute assignments, TypeError is raised EOFError Raised when built-in function input() or raw_input() hits an EOF without reading any data Methods file.read() and file.readline() return an empty string when they hit EOF FloatingPointError Raised when a floating point operation fails

GeneratorExit Raise when a generator‘s close() method is called Directly inherits from BaseException instead of StandardError since it’s technically not an error IOError Raised when an I/O operation fails for an I/O-related reason Derived from EnvironmentError ImportError Raised when an import statement fails to find the module definition or when a from... import fails to find a name to be imported IndexError Raised when a sequence subscript is out of range KeyError Raised when a dictionary key isn’t found

KeyboardInterrupt Raised when user hits the interrupt key (normally Control-C or Delete) Inherits from BaseException, so not accidentally caught by code that catches Exception MemoryError Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects) Associated value is a string indicating what kind of (internal) operation ran out of memory NameError Raised when a local or global name isn’t found Associated value is a message that includes the name not found NotImplementedError Derived from RuntimeError In user defined base classes, abstract methods should raise this when they require derived classes to override the method

OSError Derived from EnvironmentError Raised when a function returns a system-related error The errno attribute is a numeric error code The strerror attribute is the corresponding string For exceptions involving a file system path (e.g., chdir() ), the instance contains a 3 rd attribute: filename, the file name passed to the function OverflowError Raised when the result of an arithmetic operation is too large RuntimeError Raised when an error doesn’t fall in any other category Associated value is a string indicating the precisely problem Mostly a relic

StopIteration Raised by an iterator‘s next() method to signal no further values Derived from Exception rather than StandardError : not really an error SyntaxError Raised when the parser encounters a syntax error May occur  in an import statement,  in an exec statement,  in a call to the built-in function eval() or input(), or  when reading the initial script or standard input (also interactively) Instances have attributes filename, lineno, offset, and text SystemError Raised when the interpreter finds an internal error, but there’s still hope Associated value is a string indicating what went wrong (in low-level terms). Report this to the maintainer of your Python interpreter

SystemExit Raised by the sys.exit() function If the associated value is a plain integer, it specifies the system exit status (passed to C’s exit() function)  If it’s None, exit status is 0  If it has another type (e.g., a string), the object’s value is printed and the exit status is 1 A call to sys.exit() is translated into an exception so that  finally clauses can be executed, and  a debugger can execute a script without losing control Inherits from BaseException instead of StandardError or Exception so it isn’t accidentally caught by code catching Exception

TypeError Raised when an operation/function applied to an object of inappropriate type Associated value is a string giving details about the type mismatch UnboundLocalError Raised when a reference is made to a local variable in a function or method, but no value bound to that variable Subclass of NameError UnicodeError Raised when a Unicode-related encoding or decoding error occurs Subclass of ValueError UnicodeEncodeError Raised when a Unicode-related error occurs during encoding Subclass of UnicodeError UnicodeDecodeError Raised when a Unicode-related error occurs during decoding Subclass of UnicodeError

UnicodeTranslateError Raised when a Unicode-related error occurs during translating Subclass of UnicodeError ValueError Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value WindowsError Raised when a Windows-specific error occurs or when the error number doesn’tt correspond to an errno value Subclass of OSError ZeroDivisionError Raised when the 2 nd argument of a division or modulo operation is 0 Associated value is a string indicating the type of the operands and the operation

Exceptions used as warning categories Warning Base class for warning categories UserWarning Base class for warnings generated by user code DeprecationWarning Base class for warnings about deprecated features PendingDeprecationWarning Base class for warnings about features to be deprecated in the future SyntaxWarning Base class for warnings about dubious syntax

RuntimeWarning Base class for warnings about dubious runtime behavior FutureWarning Base class for warnings about constructs that will change semantically in the future ImportWarning Base class for warnings about probable mistakes in module imports UnicodeWarning Base class for warnings related to Unicode