Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.

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

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
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.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
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.
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 in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Java Software Solutions Foundations of Program Design Sixth Edition
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Object Oriented Programming
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.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
Chapter 13 Exception Handling
Introduction to Exceptions in Java
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 14: Exception Handling
Exceptions 10-Nov-18.
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exceptions Handling the unexpected
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Abdulmotaleb El Saddik University of Ottawa
Exception Handling in Java
Exception Handling Chapter 9 Edited by JJ.
Fundamental Error Handling
Web Design & Development Lecture 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 12: Exceptions and Advanced File I/O
Lecture 11 Objectives Learn what an exception is.
Exceptions 10-May-19.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling and Event Handling
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 2 Outline Intro. To Exceptions in Java Java Exception Handling Try, catch, finally blocks throw statement and throws clause How to create your own exception classes Ex. AgeException classes Exceptions in GUI

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 3 Intro. to Exceptions in Java Run-time errors happen User enters incorrect input Resource is not available (ex. file) Logic error (bug) that was not fixed For Production software Having a program "crash" is a HIGHLY UNDESIRABLE thing Users think software is no good Lose confidence

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 4 Intro. to Exceptions in Java Exception: An occurrence of an erroneous, unusual or unexpected event in a program execution In older languages Code the handling of exceptions into each area of the program that needed it Some exceptions could not even be handled by the HLL  ex. standard Pascal cannot handle I/O errors or division by 0  Ask for integer and user enters a text string – what do you do?

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 5 Intro. to Exceptions in Java In newer languages Exception handling built into the language We can separate exception handling from the "main line" code Java uses the same exception handling model used in C++ Exceptions are objects that are thrown and catched Some exceptions are built into the language Others can be created and thrown by the programmer

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 6 Exceptions in Java Java exception handling Exceptions are handled using try-catch blocks try { // code that will normally execute } catch (ExceptionType1 e) { // code to "handle" this exception } catch (ExceptionType2 e) { // code to "handle" this exception }... // can have many catches finally { // code to "clean up" before leaving try block }

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 7 Exceptions in Java If all goes well (no exceptions occur) Code in try block is executed, followed by code in (optional) finally block If an exception occurs anywhere in the try block Execution immediately jumps out of the try block An exception handler is sought in a catch block If exception is handled in a catch block, that block executes; if not, exception is propagated Whether exception is handled or propagated, finally block is executed

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 8 Exceptions in Java If an exception is handled Execution resumes immediately AFTER try/catch block in which it was handled, and does NOT return to throw point termination model of exception handling  As opposed to a resumption model, where execution resumes from where the exception occurred If an exception is propagated A handler is searched for by backing up through the call chain on the run-time stack This is dynamic exception propagation If no handler is ever found  Console applications crash and report exception  GUI applications will continue to execute, but may be in an inconsistent state – more soon

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 9 Exceptions in Java Checked vs. Unchecked exceptions Checked exceptions If a method does NOT handle these, the method MUST state that it throws them  Done in a throws clause in the method header These include IOException, and InterruptedException (and their subclasses) Unchecked exceptions Method not required to explicitly "throw" these These include RunTimeException and Error

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture Exceptions in Java Catching exceptions Catching a superclass of an exception will catch subclass exception objects catch (Exception e)  "catch all" if no other exceptions match Should list exceptions in order of most specific to most general  If catch above is first NO OTHER catches in the block could ever execute It is better style to be as specific as possible with the exceptions that are caught See ex26.java

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture throw statement & throws clause throw statement Causes an exception to be thrown. throws clause A method that throws an exception within it must catch that exception or have that exception declared in its throws clause When multiple exceptions are to be put in one throws clause, use commas to separate them int readModel(String filename) throws IOException, InterruptedException

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture Methods available to Exceptions getMessage() Ex. e.getMessage(); printStackTrace() toString() getLocalizedMessage() fillInStackTrace()

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture See Ex. AgeException The class hierarchy for the AgeException classes Exception AgeException IllegalAgeFormatExceptionOutOfAgeLimitException NegativeAgeException TooOldExceptionTooYoungException How to Create Your Own Exception Classes

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture Exceptions in GUIs GUIs run using multiple execution threads A thread is a logically separate execution chain that shares the same data See board Events in GUIs are generated and handled by threads In future courses you will see how to use threads yourselves For now we just want to know the effect of exceptions on applications that have multiple threads

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture Exceptions in GUIs If the thread in which the exception was thrown does not handle it, the thread will terminate However, other threads will continue the execute, so GUI may continue to run This does NOT mean that it will run correctly The exception may have caused a problem that persists in the GUI Don't think that because the window didn't close that everything is ok It is best to always try to anticipate and handle exceptions in GUIs See MiniCalcTwo.java, DoMathInt.java, DoMathIntCheck.java