CS1110 07 April 2008 Exceptions in Java. Today’s reading: Ch. 10. Next lecture’s reading: Ch 17. (Most) on-duty consultants now identifiable by stylish.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
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.
COMP 121 Week 5: Exceptions and Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Java Exceptions. Exceptions Often in computing, operations cannot properly execute because some sort of error has occurred. Some examples: Often in computing,
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
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.
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.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
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.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
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.
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 CIS 304 Intermediate Java Programming for Business.
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.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Intermediate Java II Sakir YUCEL MISM/MSIT Carnegie Mellon University Lecture: Exception Handling Slides adapted from Prof. Steven Roehrig.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
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:
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Slides Credit Umair Javed LUMS Web Application Development.
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 }
Class Design: Handling Errors Reading: 2 nd Ed: Chapter 15 3 rd Ed: Chapter 11 Exercises 2 nd Ed: P15.5, P15.6 (Hint: look at documentation for Scanner.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
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.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
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: When things go wrong
Recitation 2 Exception handling.
MIT AITI 2003 Lecture14 Exceptions
Exceptions 10-Nov-18.
Advanced Programming Behnam Hatami Fall 2017.
ATS Application Programming: Java Programming
Chapter 12 Exception Handling and Text IO
Exception Handling in Java
Exception Handling and Reading / Writing Files
Lecture 9: Exceptions in Java CS201j: Engineering Software
Chapter 12 Exception Handling and Text IO Part 1
Java Basics Exception Handling.
Presentation transcript:

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 headgear. Prelim 3 next Tuesday (April 14), 7:30-9pm, Ives 305 See “about Prelim III” handout Pick up graded quizzes from last time (up front) There is a review session Sunday April 12 th, 1-3pm, Phillips 101. Slides will be posted on the website. Prelim 3 is, like Prelim 2, cumulative: Look over your Prelim 2 and the P2 solutions (posted to the website, with fix of edit error in solution to last question). Uncollected P2s can be retrieved from Upson 360, M-F 10am- noon and 2-4pm with ID card. FYI: we promised to tell you: the first weighted-die code we saw (March 10) maintains the invariant “Either r >= iStart or r is in segment i-1, where iStart is the start of segment i”. A6 due Saturday. 1

Today’s topic: when things go wrong (in Java) Q: What happens when an error cause the system to abort? Important example: a “regular person” enters malformed input. Understanding this helps you debug. (NullPointerException, ArrayIndexOutOfBoundsException, …) Q: What if termination isn’t the right thing to do? It is better to warn and re-prompt the user than to have the program crash (even if they didn’t follow your exquisitely clear directions). 2 Understanding this helps you write more flexible code.

errors (little e) cause Java to throw a Throwable object Throwable a0 “/ by zero” detailMessage backtrace … Exception RuntimeException ArithmeticException Errors are signals that things are beyond help. Exceptions are signals that help may be needed; they can be “handled”. Throwable ExceptionError RuntimeException ArithmeticException …… …… OutOfMemoryError … 3

/** Illustrate exception handling */ public class Ex { public static void first() { second(); } public static void second() { third(); } public static void third() { int x= 5 / 0; } Ex.first(); AE a0 AE a0 AE a0 4 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) System prints the call-stack trace on catching exception: The Throwable is thrown to successive “callers” until caught. Here, Java catches it because nothing else does.

Execute the try-block. If it finishes without throwing anything, fine. If it throws an ArithmeticException object, catch it (execute the catch block); else throw it out further. /** = recip(x) + recip(x*x), or -1 if x is 0*/ public static double computeResult(int x) { return recip(x) + recip(x*x); } try { } catch (ArithmeticException ae) { return -1; } 5 Try-statements catch and handle Throwables. /** = reciprocal of x. Thows an ArithmeticException if x is 0. (suppose this is third-party code that you can’t change)*/ public static double recip(int x) { …; }

/** = recip(x) + recip(x*x), or -1 if x is 0*/ public static double computeResult(int x) { return recip(x) + recip(x*x); } If (x != 0) { } else { return -1; } 6 Try-statements vs. if-then checking This was meant to be a small example. Use your judgment: For (a small number of) simple tests and “normal” situations, if- thens are better. If the caller, not the method itself, should decide what should be done, throw an exception (like recip() does). There are some natural try(catch) idioms…

/** 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"); } Ex.first(); 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 7 We can create new Throwable objects ourselves.

We can write our own Exception subclasses, but we may need a “throws” clause to compile /** 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"); } Don’t worry about whether to put a throws clause in or not. Just put it in when it is needed in order for the program to compile. 8