CS1110 20 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
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

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
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.
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.
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.
COMP 121 Week 5: Exceptions and Exception Handling.
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.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
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.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
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 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.
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.
©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.
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.
CS1101: Programming Methodology Aaron Tan.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
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.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
Review Session for EXCEPTIONS & GUIS -David Gries 1 Adapted from Previous Review Slides.
To write with a broken pencil is pointless. He said I was average, but he was just being mean.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
COMP Exception Handling Yi Hong June 10, 2015.
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.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency.
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.
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.
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.
Exceptions: When things go wrong
Recitation 2 Exception handling.
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Review Session for EXCEPTIONS & GUI -Deepak Bapat
Exceptions & exception handling
Exceptions & exception handling
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.
Chapter 12 Exception Handling and Text IO Part 1
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.

Please check that your grades on CMS match what you think they are. No lab assignment today or tomorrow. But the TAs and consultants will be in the labs in order to (1) help you with questions about the prelim tonight and (2) help you with assignment A7. The final exam will be Thursday, 13 May, 9:00- 11:30AM, Barton East. We are scheduling review sessions for study week, May.

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)

Exceptions and Errors In Java, there is a class Throwable: Throwable a0 “/ by zero” detailMessage getMessage() 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

/** 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(); 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

/** 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) Class: Call Output AE a0 AE a0 AE a0

/** 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"); } 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.

Catching a thrown exception public static void first() throws MyException{ second(); 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. try { } catch (MyException ae) { System.out.println("Caught MyException: " + ae); }