Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.

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

1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
COMP 121 Week 5: Exceptions and Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
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.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
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 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.
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 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.
©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.
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.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Program Errors Syntax errors Logic errors
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.
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.
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
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.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
COMPSCI 230 S Programming Techniques
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
ATS Application Programming: Java Programming
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Chapter 12 Exception Handling and Text IO Part 1
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exceptions 10-May-19.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Presentation transcript:

Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements –propagation –throws clause –finally

Dealing with Errors - Exceptions Example: if (n > 0) avg = total / n; else avg = ??? (what to do) idea: identify situations where error occurs –if error occurs, “throw” an exception –have routines to “catch” (deal with) exception

Exception Classes Lots! java.lang.ArithmeticException java.lang.IndexOutofBoundsException (parent of java.lang.ArrayIndexOutofBoundsException java.lang.StringIndexOutofBoundsException) java.lang.NegativeArraySizeException java.lang.NullPointerException java.io.IOException java.io.InterrruptedIOException java.io.EOFException java.io.FileNotFoundException...

Some Methods with Exceptions IllegalArgumentException Choice String getItem(int index) - if index out of range void select(int index) - same problem Container void add(Component c) - trying to add c to itself Dialog Dialog(Frame parent) - parent is null Label void setAlignment(int a) - a not Label.CENTER, etc. TextArea void setColumns(int c) - if c negative void setRows(int r) - if r negative

More Exceptions NumberFormatException Double static double valueOf(String s) - s not double - also for float, int, long NullPointerException Choice void addItem(String s) - string is null almost all String methods that take String args etc.

Dealing with Exceptions Ignore: will cause program to terminate Write code so they don’t occur: sometimes difficult Idea: recognize when they occur and add ways to deal with them

Try Blocks Format: try { // code with possible exception } Section of code you think may raise an exception If an exception occurs within a try block, the block terminates Java then looks for a “catch” statement following try block dealing with exception

Catch Statement Format: catch (ExceptionClass name) { // } Immediately follows try May be more than one –if > 1, first to match is used Matched by class of exception thrown name attached to instance associated with exception

Exception Example try { avg = total / n; // go on to use avg in output // if exception raised these statements skipped } catch (ArithmeticException e) { // indicate there is a problem }

Multiple Catch Statements If more than one catch, first to match Exception class used Be careful to list them in the order you want them to apply try { } catch (Exception e) { // catches anything } catch (ArithmeticException e) { // pointless }

Propagating Exceptions If no case matching exception raised after try, exception propagates until caught –may propagate out of methods (sometimes) try { // exception thrown } catch (DifferentExceptionType e) { // not caught } catch (NeededExceptionType e) { // caught here }

Throws Clause Methods may have attached throws clause –Method header: Type methName(args) throws EType1, EType2, … Important for “checked” exceptions –checked exceptions may not propagate out of method unless included in throws clause –samples: IOExceptions, your own exceptions –generally, any subclass of RunTimeExceptions not checked

Using Your Own Exceptions Two ctors: ExceptionClass() ExceptionClass(String message) - associate message with exception (used when caught) Throwing: throw myExceptionInstance; Remember to see if the Class you are using is checked

void showEmployee(Employee[] bees, int MaxEmp, int index) { try { if ((index = MaxEmp)) throw new ArrayIndexOutOfBounds(“No employee with number” + String.valueOf(index)); // go ahead and show bees[index] } catch (ArrayIndexOutOfBounds e) { // dump out message }

Finally May add finally statement after last catch, finally is always executed whether catch used or not Format: finally { // stuff here } Good for cleanup code (deallocating temporary space set aside with new, etc.)