Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Slides:



Advertisements
Similar presentations
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 9 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
Catching Exceptions An exception may be thrown by a method when an exceptional occurance happens (and the method does not have the expertise to remedy.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
Chapter 11  I/O and Exception Handling 1 Chapter 11 I/O and Exception Handling.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exception Handling. Lecture Objectives To learn how to throw exceptions To be able to design your own exception classes To understand the difference between.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Applets & Applications CSC 171 FALL 2001 LECTURE 15.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
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 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.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
Chapter 14 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
The Problem Unexpected erroneous situations are often discovered by code which is NOT prepared to remedy the error………. For example.. String in = JOptionPane.showInputDialog(“enter.
Exception Handling.
Java Exceptions a quick review….
Introduction to Exceptions in Java
CSE 501N Fall ’09 17: Exception Handling
Advanced Java Programming
ATS Application Programming: Java Programming
Exceptions Handling the unexpected
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
Exception Handling.
Web Design & Development Lecture 7
Chapter 11 Input/Output Exception Handling
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling Contents
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Presentation transcript:

Exceptions CSC 171 FALL 2004 LECTURE 24

READING Read Horstmann Chapter 14 This course covered Horstmann Chapters

EXAM Thursday 12/9 in class – Chapters 11, 13-15

Make up exam Friday 12/3 12:15PM-1:25PM CSB 703

GRADING OLD Midterm 10% Projects (4) 40% Final 15% Quizes (10) 10% Labs (15) 15% Workshops10% NEW Exam 1 13% Projects (4) 40% Exam 2 12% Quizes (6) 5% Labs (21) 20% Workshops10%

Errors in Programming

Sometimes cause by our code Sometimes caused by external code Reasonable to take precautions

Two aspects of errors Recognizing when an error occurs Dealing with the errors

There are two aspects of handling failure: ________________ and ________________.

There are two aspects of handling failure: ___detection____ and ____recovery____________.

Old School have the method return an indication of success/failure (an “error code”) x.doSomething(); // becomes if (x.doSomething() == -1) return false;

New School Java has an exception handling mechanism which can require potential errors to be recognized. This mechanism is flexible and efficient.

In Java, ___________________________ provides a flexible mechanism for passing control from the point of error detection to a recovery handler.

In Java, __exception handling_______ provides a flexible mechanism for passing control from the point of error detection to a recovery handler.

Exception Objects

THROWING EXCEPTIONS If I am a method AND If a problem occurs then I can deal with it by : 1. Constructing a new exception object describing the problem 2. “Throw” the object to the method that invoked me (I have to terminate to do this)

Example public class BankAccount { public void withdraw(double amount) { if (amount > balance) throw new IllegalArgumentException( "Amount exceeds balance"); balance = balance - amount; }... }

To signal an exceptional condition, use the _____________ statement to throw an _____________________ object.

To signal an exceptional condition, use the ____throw____ statement to throw an __________exception____ object.

When you throw an exception, the current method _____________________.

When you throw an exception, the current method __terminates______.

CHECKED/UNCHECKED It’s hard to anticipate all possible exceptions at compile time. The compiler checks to see if we deal with exceptions (checked exeptions) Unchecked exceptions are not enforced by the compiler.

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException ; public class Console { public static void main(String [] args) { InputStreamReader isreader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(isreader); String input1 = console.readLine(); System.out.println(input1); }

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException ; public class Console { public static void main(String [] args) throws IOException{ InputStreamReader isreader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(isreader); String input1 = console.readLine(); System.out.println(input1); }

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException ; public class Console { public static void main(String [] args) { InputStreamReader isreader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(isreader); try { String input1 = console.readLine(); System.out.println(input1); } catch (IOException e) { System.out.println(“Problem”); }

Unchecked Exceptions int k = Integer.parseInt(“Hello World”); int [] a = {2,3,4,5,6,7,8,9} ; a[20] = 5;

There are two kind of exceptions: ______________________ and _____________________ exceptions.

There are two kind of exceptions: ___checked_______ and ___unchecked________ exceptions.

Unchecked exceptions extend the class _________________________ or _________________.

Unchecked exceptions extend the class _RuntimeException______ or _____Error______.

Checked exceptions are due to __________________________________. The compiler checks that your program handles these exceptions.

Checked exceptions are due to _external circumstances___. The compiler checks that your program handles these exceptions.

Add a _____________________ specifier to a method that can throw a checked exception.

Add a __throws_______ specifier to a method that can throw a checked exception.

You can design your own exception types – subclasses of _______________________ or ______________________.

You can design your own exception types – subclasses of __Exception____ or ___RuntimeException_.

public class InsufficientFundsException extends RuntimeException { public InsufficientFundsException() { } public InsufficientFundsException(String reason) { super(reason); } }

TRY/CATCH Statements in try block are executed If no exceptions occur, catch clauses are skipped If exception of matching type occurs, execution jumps to catch clause If exception of another type occurs, it is thrown to the calling method If main doesn't catch an exception, the program terminates with a stack trace

try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.println("How old are you?"); String inputLine = in.readLine(); int age = Integer.parseInt(inputLine); age++; System.out.println("Next year,you'll be " + age); } catch (IOException exception) { System.out.println("Input/output error " +exception); } catch (NumberFormatException exception) { System.out.println("Input was not a number"); }

In a method that is ready to handle a particular exception type, place the statements that can cause the exception inside a ____________________, place the handler inside a ______________________________.

In a method that is ready to handle a particular exception type, place the statements that can cause the exception inside a ________try block________, place the handler inside a __catch clause_________.

It is better to _________________________________ than to _______________________.

It is better to ________give_______________ than to ________ receive _________.

finally Exception terminates current method Danger: Can skip over essential code Example: BufferedReader in; in = new BufferedReader( new FileReader(filename)); purse.read(in); in.close(); Must execute in.close() even if exception happens Use finally clause for code that must be executed "no matter what"

Once a try block is entered, the statements in a ___________________ clause are guaranteed to be executed, whether or not an exception is thrown.

Once a try block is entered, the statements in a ______ finally _____ clause are guaranteed to be executed, whether or not an exception is thrown.

Example BufferedReader in = null; try { in = new BufferedReader( new FileReader(filename)); purse.read(in); } finally { if (in !=null) in.close(); }