Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing.
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.
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.
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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handler catches a raised/thrown exception try.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
Exceptions COMPSCI 105 S Principles of Computer Science.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
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.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
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?
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions COMPSCI 105 SS 2015 Principles of Computer Science.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
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.
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.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exception Handling How to handle the runtime errors.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
George Mason University
Example: Vehicles What attributes do objects of Sedan have?
Why exception handling in C++?
Handling errors try except
Exceptions and files Taken from notes by Dr. Neil Moore
Exceptions with Functions
Exception Handling.
Python’s Errors and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
Throwing and catching exceptions
Exception Handling By: Enas Naffar.
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Exceptions.
Exceptions 1 CMSC 202.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Object-Oriented Programming (OOP) Lecture No. 43
By Ryan Christen Errors and Exceptions.
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
IST256 : Applications Programming for Information Systems
Dealing with Runtime Errors
Presentation transcript:

Exceptions CMSC 201

Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example 2: user enters "garbage" data  example 3: disk full

Vocabulary When some piece of code causes a run-time error, we say that the code throws an exception or that it raises an exception. The part of a program that deals with the run-time error catches the exception or handles the exception.

Divide by Zero totalBill = 67 n = int(input("Number of people? ")) share = totalBill / n print("Share of the bill is ", share) If user enters 0, Python complains and terminates: Traceback (most recent call last): File "divide_by_zero.py", line 3, in share = totalBill / n ZeroDivisionError: division by zero

Exception Handling try: totalBill = 67 n = int(input("Number of people? ")) share = totalBill / n except ZeroDivisionError : print("Customers ran away") else: print("Share of the bill is ", share)

Syntax for Exceptions try: block of code that might cause one or more types of exceptions except ExceptionType1 : block of code to handle ExceptionType1 except ExceptionType2 : block of code to handle ExceptionType2... else: block of code to execute when no exceptions found

Exception Types

Badgering the user for input done = False while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") else: print("Thank you!") done = True print("n is ", n)

Badgering the user for input

done = False while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") except EOFError: print("Please type something! Try again.") else: print("Thank you!") done = True print("n is ", n)

Raising an Exception You can write code that raises exceptions: try: raise ZeroDivisionError except ZeroDivisionError: print("Did someone divide by zero?") else: print("Everything is hunky-dory") More useful later when we look at functions

BaseException The BaseException type matches all exceptions, even ones you don't know about. Use this very carefully! Might not be a good idea. What can you do if you catch a BaseException? o exit the program slightly more gracefully. o return to home state (if this is possible). o re-throw the exception (requires more syntax and not clear what is accomplished).