11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Handling Errors Introduction to Computing Science and Programming I.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Exceptions COMPSCI 105 S Principles of Computer Science.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
1 CSC241: Object Oriented Programming Lecture No 27.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
General Programming Introduction to Computing Science and Programming I.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
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 in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Exception Handling How to handle the runtime errors.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
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.
Java Exceptions a quick review….
C++ Exceptions.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Example: Vehicles What attributes do objects of Sedan have?
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Python: Control Structures
Handling Exceptions.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Exceptions with Functions
Exception Handling.
Python’s Errors and Exceptions
Abdulmotaleb El Saddik University of Ottawa
ERRORS AND EXCEPTIONS Errors:
3. Decision Structures Rocky K. C. Chang 19 September 2018
Topics Introduction to File Input and Output
Presentation transcript:

11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)

Objectives To understand the idea of exception handling and be able to write simple exception handling code that catches standard Python run-time errors.

Exception Handling Various error messages can occur when executing Python programs. Such errors are called exceptions. An exception is a value (object) that is raised (“thrown”) signaling that an unexpected, or “exceptional,” situation has occurred. Python contains a predefined set of exceptions referred to as standard exceptions.

EXERCISE 11.1 Try List = [1,2,3]; List[3] file = open("unknown.py", "r") x = 3 + "4" in = 10 int("10.4") import maths

Standard exceptions in Python Source: Charles Dierbach Introduction to Computer Science Using Python. Wiley. See for all built-in exceptions

EXERCISE 11.2 Find a Python file that has functions. Try to make an error inside the function and observe the error messages reported to you.

Propagation of Raised Exceptions Source: Charles Dierbach Introduction to Computer Science Using Python. Wiley.

Propagation of Raised Exceptions An exception is either handled by the client code, or automatically propagated back to the client’s calling code, and so on, until handled. If an exception is thrown all the way back to the main module (and not handled), the program terminates displaying the details of the exception.

EXERCISE 11.3 Incur an error message from math.factorial(-1). Now try try: math.factorial(-1) except ValueError: print("Sorry, factorial() does not admit negative number.")

Catching and Handling Exceptions The try statement has the following form: try: except : Exceptions are caught and handled in Python by use of a try block and exception handler. When Python encounters a try statement, it attempts to execute the statements inside the body. If there is no error, control passes to the next statement after the try … except. Else, Python looks for an except clause with a matching error type. If one is found, the handler code is executed.

EXERCISE 11.4 Consider while True: number = eval(input("Enter a positive number: ")) if number >= 0: break # Exit loop if number is valid Use Try-Except block to catch the errors when the user is not entering a number. An error message will be printed out.

EXERCISE 11.5 Now we want to improve the codes for exercise If a user is not entering a number, in additional to the error message, he will be prompted to enter a positive integer until he enters a positive number.

EXERCISE 11.6 Now we impose a range of numbers (say from 0 to 9, inclusive) acceptable to the program in Exercise 11.5.

EXERCISE 11.7 Now you are given the function below: def getNumber(): number = eval(input("Enter a number between 0 and 9, inclusive: ")) if number 9: raise ValueError("The input must be between 0 and 9, inclusive.") return number Use this function to solve Exercise 11.6.

EXERCISE 11.8 If you have been using eval() all along, replace eval() with int(). Any difference between the error messages received here and those in the previous exercise?

EXERCISE 11.9 Replace your except handler with except ValueError as err_mesg: print(err_mesg) and use int(). Observe the difference.

END