CS1110 19 April 2010 Exceptions in Java. Read chapter 10. HUMOR FOR LEXOPHILES (LOVERS OF WORDS): Police were called to a day care; a three-year-old was.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Recitation 4. 2-D arrays. Exceptions. Animal[] v= new Animal[3]; 2 declaration of array v v null Create array of 3 elements a6 Animal[] null Assign.
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
For use of Cleveland State's IST410 Students only 1 Exception.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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.
CS1110 Lec November 2010 Exceptions in Java 1 Reading for today: 10. Next lecture: Ch 9.3 No labs this week, no TA office hours Wed-Fri, see consultant.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
CS November 2009 Exceptions in Java. Prof. Gries is lecturing in India for the next 7 days or so. Today’s reading: Ch. 10. Next lecture’s reading:
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
©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.
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.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Review Session for EXCEPTIONS & GUIS -David Gries 1 Adapted from Previous Review Slides.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
CS April 2010 Exceptions in Java. Read chapter 10. HUMOR FOR LEXOPHILES (LOVERS OF WORDS): Police were called to a day care; a three-year-old was.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
CS April 2008 Exceptions in Java. Today’s reading: Ch. 10. Next lecture’s reading: Ch 17. (Most) on-duty consultants now identifiable by stylish.
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
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 in the Java programming language J. W. Rider.
Chapter 14 – Exception Handling
Exceptions: When things go wrong
Recitation 2 Exception handling.
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
MIT AITI 2003 Lecture14 Exceptions
Review Session for EXCEPTIONS & GUI -Deepak Bapat
Exceptions & exception handling
Exceptions & exception handling
Abdulmotaleb El Saddik University of Ottawa
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Chapter 12 Exception Handling and Text IO Part 1
CMSC 202 Exceptions.
Presentation transcript:

CS April 2010 Exceptions in Java. Read chapter 10. HUMOR FOR LEXOPHILES (LOVERS OF WORDS): Police were called to a day care; a three-year-old was resisting a rest. Did you hear about the guy whose whole left side was cut off? He's all right now. The butcher backed into the meat grinder and got a little behind in his work. When fish are in schools they sometimes take debate. A thief fell and broke his leg in wet cement. He became a hardened criminal. Thieves who steal corn from a garden could be charged with stalking. When the smog lifts in Los Angeles, U.C.L.A.

What happens when an error of some sort occurs? // String s is supposed to contain an integer. // Store that integer in variable b. b= Integer.parseInt(s); /** Parse s as a signed decimal integer and return the integer. If s does not contain a signed decimal integer, throw a NumberFormatException. */ public static int parseInt(String s) parseInt, when it find an error, does not know what caused the error and hence cannot do anything intelligent about it. So it “throws the exception” to the calling method. The normal execution sequence stops!

Exceptions and Errors In Java, there is a class Throwable: Throwable a0 “/ by zero” detailMessage getMessage() Throwable() Throwable(String) When some kind of error occurs, an exception is “thrown” —you’ll see what this means later. An exception is an instance of class Throwable (or one of its subclasses)

Exceptions and Errors So many different kind of exceptions that we have to organize them. Throwable a0 “/ by zero” detailMessage getMessage() Exception RuntimeException ArithmeticException Throwable ExceptionError RuntimeException ArithmeticException Do nothing with these You can "handle" these Throwable() Throwable(String) Exception() Exc…(..) RunTimeE…() RunTimeE…(…) ArithmeticE…() ArithmeticE…(…)

02 /** Illustrate exception handling */ 03 public class Ex { 04 public static void first() { 05 second(); 06 } public static void second() { 09 third(); 10 } public static void third() { 13 int x= 5 / 0; 14 } 15 } ArithmeticException: / by zero at Ex.third(Ex.java:13) at Ex.second(Ex.java:9) at Ex.first(Ex.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(…) at sun.reflect.DelegatingMethodAccessorImpl.invoke(…) at java.lang.reflect.Method.invoke(Method.java:585) Class: Call Output AE a0 AE a0 AE a0 Ex.first();

/** Illustrate exception handling */ public class Ex { public static void first() { second(); } public static void second() { third(); } public static void third() { throw new ArithmeticException ("I threw it"); } ArithmeticException: I threw it at Ex.third(Ex.java:14) at Ex.second(Ex.java:9) at Ex.first(Ex.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(…) at sun.reflect.DelegatingMethodAccessorImpl.invoke(…) at java.lang.reflect.Method.invoke(Method.java:585) AE a0 AE a0 AE a0 Class: Call Output Ex.first();

/** An instance is an exception */ public class OurException extends Exception { /** Constructor: an instance with message m*/ public OurException(String m) { super(m); } /** Constructor: an instance with no message */ public OurException() { super(); }

/** Illustrate exception handling */ public class Ex { public static void first() { second(); } public static void second() { third(); } public static void third() { throw new MyException("mine"); } Ex.first(); ArithmeticException: mine at Ex.third(Ex.java:14) at Ex.second(Ex.java:9) at Ex.first(Ex.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(…) at sun.reflect.DelegatingMethodAccessorImpl.invoke(…) at java.lang.reflect.Method.invoke(Method.java:585) Class: Call Output Won’t compile. Needs a “throws clause, see next slide

The “throws” clause /** Class to illustrate exception handling */ public class Ex { public static void first() throws MyException { second(); } public static void second() throws MyException { third(); } public static void third() throws MyException { throw new MyException("mine"); }

Catching a thrown exception public class Ex1 { public static void first() throws MyException{ try { second(); } catch (MyException ae) { System.out.println ("Caught MyException: " + ae); } System.out.println ("procedure first is done"); } public static void second() throws MyException { third(); } public static void third() throws MyException { throw new MyException(”yours"); } Execute the try- block. If it finishes without throwing anything, fine. If it throws a MyException object, catch it (execute the catch block); else throw it out further.

/** Input line supposed to contain one int, maybe whitespace on either side. Read line, return the int. If line doesn’t contain int, ask user again */ public static int readLineInt() { String input= readString().trim(); // inv: input contains last input line read; previous // lines did not contain a recognizable integer. while (true) { try { return Integer.valueOf(input).intValue(); } catch (NumberFormatException e) { System.out.println( "Input not int. Must be an int like"); System.out.println("43 or -20. Try again: enter an int:"); input= readString().trim(); }