Exceptions COMP53 Sept 7 2007. Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Exception Handling Xiaoliang Wang, Darren Freeman, George Blank.
For use of Cleveland State's IST410 Students only 1 Exception.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
COMP201 Java Programming Topic 6: Exceptions Reading: Chapter 11.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
1 Why do we need exceptions? In C, return variables must be used to indicate errors: if((fd = fopen(path,...)) == -1){ if(errno==a){...} else if(errno==b){...}
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
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.
Introduction to Computer Programming Error Handling.
CS203 Java Object Oriented Programming Errors and Exception Handling.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
Exceptions Handling Exceptionally Sticky Problems.
Exception Handling Part 2: Creating and Throwing Exceptions CSIS 3701: Advanced Object Oriented Programming.
Practice Session 9 Exchanger CyclicBarrier Exceptions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
Creating a GUI Class An example of class design using inheritance and interfaces.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
Exception Handling How to handle the runtime errors.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Chapter 10 – Exception Handling
Handling Exceptionally Sticky Problems
Introduction to Exceptions in Java
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Handling Exceptionally Sticky Problems
Chapter 11: Exception Handling
Presentation transcript:

Exceptions COMP53 Sept

Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions allows for a clean way to escape from a problem, without complication the normal control logic. Throwing an exception says: “I can’t do this, so I’m throwing the ball back to someone else.

Exception Metaphor Bill passes a task to Jane Jane passes the task on to Jerry Jerry has a problem with the task, so he throws it back to Jane. If Jane can handle the problem, she does so, otherwise she throws it back to Bill. If Jane can handle the problem, he does so, otherwise he gives up on it.

Exception Objects Java provides a general purpose Exception class. – Often this is sufficient We can also define special purpose Exceptions as subclasses of Exception – This allows us to be more selective in handling exceptions

Creating an Exception Class public class DivideByZeroException extends Exception { DivideByZeroException(String _msg) { super(_msg); } All exceptions can carry a message (String) to give information back to the receiver of the exception.

Throwing Exceptions The throw command is used to throw exceptions. A new exception object must be created when it is thrown. Exceptions that might be thrown by a method must be identified in the method signature.

Method Throwing an Exception private static double divideThese(double x, double y) throws DivideByZeroException { if (y==0.0) throw new DivideByZeroException("in divideThese()"); double z = x/y; return z; } If the exception is thrown, control is immediately returned to the method that called this method.

Catching an Exception Anyone calling a method that might throw an exception must be prepared to catch it. This requires a try-catch block. – Try to do something that might throw an exception – If there is no exception, proceed normally – If there is an exception, immediately jump to code that will deal with it.

Catching an Exception try { double a = divideThese(10,0); System.out.println("Answer is "+a); } catch (DivideByZeroException dbz) { System.out.println(dbz); } If a DivideByZeroException is thrown during the call to divideThese(), control will immediately jump to the code in the catch block.

When to Use Exceptions We’ll primarily use exceptions to indicate that something has gone wrong inside of a data structure. Examples: – an attempt to access an array with an out of bounds index – trying to extract (pop) an object from an empty queue