4/29/20151 Java Exceptions. 4/29/20152 Overview An exception is an unusual circumstance, such as an error condition, that must be handled in a non-standard.

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

Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Topics Introduction Types of Errors Exceptions Exception Handling
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter SeventeenModern Programming Languages1 A Third Look At Java.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
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.
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.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
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.
Program Errors Syntax errors Logic errors
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
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.
Object Oriented Programming with Java (150704).  Throwable Exception (This class will catch exceptions generated by prog.) (Create your own custom exception.
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.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
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 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.
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.
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.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
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.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
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.
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 1201 Object Oriented Programming
Chapter 10 – Exception Handling
EE422C Software Implementation II
Exceptions & exception handling
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling and Text IO
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.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
CMSC 202 Exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

4/29/20151 Java Exceptions

4/29/20152 Overview An exception is an unusual circumstance, such as an error condition, that must be handled in a non-standard way. Java and many other languages (including C++), exceptions serve as a standard mechanism for handling execution error conditions. Provides for handling unusual cases at the appropriate level(s). For example, an airplane could have a stuck wing flap detected in a very low-level method monitoring a wing flap's position. No ability to communicate information directly to the pilot. A wing flap exception can be reported upward to the higher levels that call the low-level method where the exception is detected. The higher level layers, with broader status information, could automatically adjust other control surfaces and report the wing flap exception to the pilot.

4/29/20153 A Little Demo public class Test { public static void main(String[] args) { System.out.println(5/0); } } javac Test.java java Test Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.main(Test.java:3)

4/29/20154 Exception Example public class Test { public static void main(String[] args) { try { System.out.println( 5 / 0); } catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); } } } This will catch and handle any ArithmeticException. Other exceptions will still get the language system’s default behavior. javac Test.java java Test You're dividing by zero!

4/29/20155 Exception Mechanism The exception mechanism consists of three parts: 1.definition of the exception class 2.throwing the exception when exceptional condition is detected 3.catching or handling the exception (try and catch).

4/29/20156 Exercise 1A - List line numbers executed and output 1. System.out.print("1"); 2. try { 3. System.out.print("2"); 4. if (true) throw new Exception(); 5. System.out.print("3"); 6. } 7. catch (Exception e) { 8. System.out.print("4"); 9. } 10. finally { 11. System.out.print("5"); 12. } 13. System.out.println("6");

Exercise 1B - List line numbers executed and output 1.public class Exercise1 { 2. static void f1() { 3. System.out.print("1"); 4. try { 5. System.out.print("2"); 6. f2(); 7. System.out.print("3"); 8. } 9. catch (Exception e) { System.out.print("4"); } 10. finally { System.out.print("5"); } 11. System.out.println("6"); 12. } 13. static void f2 () throws Exception { 14. if (true) throw new Exception(); 15. } 16. public static void main(String s[]) { f1(); } 17.}

Exercise 1C - List line numbers executed and output 1.public class Exercise1 { 2. static void f1() throws Exception { 3. System.out.print("1"); 4. try { 5. System.out.print("2"); 6. f2(); 7. System.out.print("3"); 8. } 9. catch (Exception e) { System.out.print("4"); throw e; } 10. finally { System.out.print("5"); } 11. System.out.println("6"); 12. } 13. static void f2 () throws Exception { 14. if (true) throw new Exception(); 15. } 16. public static void main(String s[]) throws Exception { 17. f1(); 18. } 19.} 1245 Exception thread "main" java.lang.Exception at Exercise1.f2(Exercise1.java:15) at Exercise1.f1(Exercise1.java:6) at Exercise1.main(Exercise1.java:19)

4/29/20159 Define Generally, an exception class inherits from the parent class Exception. The exception class definition minimally defines a class constructor but may also provide methods for analysis of the conditions causing the exception. The following is a typical class definition for counterException with a constructor that copies a string message complaint presumably detailing the cause of the exception. An example of the constructor's use illustrates a new counterException created with the complaint that count failed.

4/29/ Define and Use Example class counterException extends Exception { String complaint; public counterException(String complaint){ this.complaint = complaint; } public String toString(){ return "counter Exception " + complaint; } } new counterException("count failed."); Define Use

4/29/ Throw When an error condition is detected, it is necessary to handle it immediately where detected or throw the exception to the calling method. Exception handling is a mechanism for transferring control from where an error occurred to where it can be handled most appropriately. After the exception is thrown, the throwing method terminates and execution control is immediately passed backward along the chain of callers from where the exception was thrown. Any method along the calling chain can: a) handle the exception and continue with execution, b) handle the exception and again throw the exception to the calling method to handle c) or do nothing, terminate immediately and let the calling method deal with the exception. The down method below is an example of throwing an exception, in this case when the counter n becomes negative.

4/29/ Throw Example public int down() throws counterException { if (n <= 0) throw new counterException( n + " count Down failed."); return --n; } The down method is an example of throwing an exception, in this case when the counter n becomes negative. If exception is thrown, execution does not reach return --n;

4/29/ Catch Handling an exception consists of trying a method that throws an exception and catching a thrown exception. Catching an exception prevents the exception from being passed to a higher level calling method. If an exception is not caught, the method terminates immediately and control is passed to the higher level calling method so that it might catch the exception. Consider the following example which causes a counterException to be thrown by method down when the counter becomes negative. The try and catch go together to define the method that throws the exception, down, and the exception to be caught, counterException. Only if an exception is thrown will it be caught and the catch statement(s) executed. Any statements following the catch would be executed as normal.

4/29/ Catch Example try { aCounter.down( ); } catch (counterException ce) { System.out.println("" + ce); } public int down() throws counterException { if (n <= 0) throw new counterException( n + " count Down failed."); return --n; } Catch Throw

Throwing exceptions to caller When an exception is not handled in a method it must be thrown to the higher level calling method. That method must state that it throws an exception as throws counterException. The down method listed earlier does not handle or catch the counterException thrown so the exception is thrown back to the calling method, action. The direct effect of down throwing an exception is to terminate its execution, passing execution to the catch, so that the remaining statements of down (i.e. return --n) are not executed. public int down() throws counterException { if (n <= 0) throw new counterException( n + " count Down failed."); return --n; }

4/29/ Finally try – Invokes a method that throws an exception. catch – Point of execution of exception throw. finally - Always executed at end of a try block.

4/29/ Exception Class Example 1.class counterException extends Exception { // Define 2. String complaint; 3. public counterException(String c) { 4. this.complaint = c; 5. } 6. public String toString( ) { 7. return "counter Exception “ + complaint; 8. } 9. } 10.class counter { 11. int n = 0; 12. public int zero() { return n=0; } 13. public int up() { return ++n; } 14. public int down() throws counterException { // Throw 15. if (n <= 0) 16. throw new counterException 17. (n + " count Down failed."); 18. return --n; 19. } 20.}

Exception Class Use Example 21.public class Example { 22. public static void main( String args[] ) { 23. counter aCounter = new counter( ); 24. aCounter.zero( ); 25. aCounter.up(); 26. try { aCounter.down( ); } 27. catch (counterException ce) { // Catch 28. System.out.println("" + ce); 29. } 30. try { aCounter.down( ); } 31. catch (counterException ce) { // Catch 32. System.out.println("" + ce); 33. } 34. finally { 35. System.out.println(“Finally”); 36. } 37. } 38.}

Exercise 2 1. List the sequence of line numbers executed and output. 1.class counterException extends Exception { 2. String complaint; 3. public counterException(String c){ this.complaint = c; } 4. public String toString( ) { return "counter Exception “ + complaint; } 5.} 6.class counter { 7. int n = 0; 8. public int zero() { return n=0; } 9. public int up() { return ++n; } 10. public int down() throws counterException { 11. if (n <= 0) throw new counterException (n + " count Down failed."); 12. return --n; 13. } 14.} 15.public class Example { 16. public static void main( String args[] ) { 17. counter aCounter = new counter( ); 18. aCounter.zero( ); 19. aCounter.up(); 20. try { aCounter.down( ); } 21. catch (counterException ce) { System.out.println("" + ce); } 22. try { aCounter.down( ); } 23. catch (counterException ce) { System.out.println("" + ce); } 24. finally { System.out.println(“Finally”); } 25. } 26.} 2. The catch is defined in main method but is executed by the throw. Is the catch visible to the throw? 3. What does the throw and catch resemble?

Exercise 2 Continued 4. List the sequence of line numbers executed and output. 5. What occurs at Line 21? 1.class counterException extends Exception { 2. String complaint; 3. public counterException(String c){ this.complaint = c; } 4. public String toString( ) { return "counter Exception “ + complaint; } 5.} 6.class counter { 7. int n = 0; 8. public int zero() { return n=0; } 9. public int up() { return ++n; } 10. public int down() throws counterException { 11. if (n <= 0) throw new counterException (n + " count Down failed."); 12. return --n; 13. } 14.} 15.public class Example { 16. public static void main( String args[] ) throws Exception { 17. counter aCounter = new counter( ); 18. aCounter.zero( ); 19. aCounter.up(); 20. aCounter.down( ); 21. aCounter.down( ); 22. System.out.println(“Completed”); 23. } 24.}