We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published byThaddeus Tatem
Modified about 1 year ago
© 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 which could cause that program to behave unreliably; each type of event that could lead to an exception is associated with a pre-defined exception class in Java; when a given event occurs, the Java run-time environment determines which exception has occurred and an object of the given exception class is generated; this process is known as throwing an exception;
© The McGraw-Hill Companies, 2006 Pre-defined exception classes in Java
© The McGraw-Hill Companies, 2006 RuntimeExceptions vs IOExceptions the RuntimeException class deals with errors that arise from the logic of a program. the IOException class deals with external errors that could affect the program during input and output. Some examples of RuntimeExceptions Converting a string into a number, when the string contains non-numeric characters ; Accessing an array using an illegal index. Some examples of IOExceptions the keyboard locking; an external file being corrupted.
© The McGraw-Hill Companies, 2006 Checked and unchecked exceptions since nearly every Java instruction could result in a RuntimeException error, the Java compiler does not flag such instructions as potentially error-prone.; consequently these types of errors are known as unchecked exceptions; the Java compiler does flag up those instructions that may generate all other types of exception (such as IOException); consequently, these kinds of errors are known as checked exceptions.
© The McGraw-Hill Companies, 2006 Handling exceptions consider a simple program that: allows the user to enter an aptitude test mark at the keyboard; informs the user if he or she has passed the test and been allowed on a given course. instead of using the Scanner class, we will devise our own class, TestException, that will contain a class method called getInteger.
© The McGraw-Hill Companies, 2006 Outline AptitudeTest class
© The McGraw-Hill Companies, 2006 Outline TestException class
© The McGraw-Hill Companies, 2006 The read method of System.in each character on the keyboard is represented by a Unicode number; the read method of System.in is a bit like the next method of the Scanner class, except that it treats the String as a series of Unicode numbers; each number is considered to be of type byte, so that the String itself is an array of bytes.
© The McGraw-Hill Companies, 2006 Coding the getInteger method This is a first attempt, it will not compile!
© The McGraw-Hill Companies, 2006 A problem with the getInteger method this class will not compile because the read method of System.in may throw a checked IOException (if the keyboard locks for example). Whenever a method that throws a checked exception, Java compiler insists that we acknowledge this exception in some way. there are always two ways to deal with an exception: deal with the exception within the method by catching it; pass on the exception out of the method by claiming it.
© The McGraw-Hill Companies, 2006 Claiming an exception the term claiming an exception refers to a given method having been marked to indicate that it will pass on an exception object that it might generate. to claim an exception we add a throws clause to our method header: here have made a decision to pass the exception on to any method that calls this getInteger method.
© The McGraw-Hill Companies, 2006 Revisiting the AptitudeTest class The problem? The main method makes a call to our getInteger method, and this method may now throw an IOException. One solution Let the main method throw this exception as well:
© The McGraw-Hill Companies, 2006 Another problem: NumberFormatException This exception is raised when trying to convert a string into a number when the string contains non-numeric characters. The compiler did not warn us about this because NumberFormatException is a subclass of RuntimeException and so is unchecked.
© The McGraw-Hill Companies, 2006 Catching an exception so for we have thrown exception objects out of the current method and up to the calling method.; another way out for an exception object is into a catch block within the method; this is known as catching an exception; in order to trap the exception object in a catch block you must surround the code that could generate the exception in a try block.
© The McGraw-Hill Companies, 2006 Syntax for using a try and catch block: as there may be more than one exception generated within a method, there may be more than one catch block below a try block - each dealing with a different exception.; within the catch block, programmers can interrogate the exception object using Exception methods
© The McGraw-Hill Companies, 2006 Re-writing the AptitudeTest class
© The McGraw-Hill Companies, 2006 The finally clause Three courses of action may now take place in a try block: 1. The instructions within the try block are all executed successfully; 2. An exception occurs within the try block; the try block is exited and a matching catch block is found for this exception; 3. An exception occurs within the try block; the try block is exited but no matching catch block is found for this exception; so the exception is thrown from the given method. it may be the case that, no matter which of these courses of action take place, you wish to execute some additional instructions before the method terminates; often such a scenario arises when you wish to carry out some cleanup code, such as closing a file or a network connection that you have opened in the try block; the finally clause allows you to do this.
© The McGraw-Hill Companies, 2006 The syntax for the finally clause
© The McGraw-Hill Companies, 2006 Exceptions in GUI applications Exceptions do not terminate GUI applications; however they may make them behave unpredictably so should still be handled.
© The McGraw-Hill Companies, 2006 Using exceptions in your own classes we have managed to avoid the need for exceptions in our own classes by using if statements to monitor abnormal situations, and to send back boolean error values when appropriate; sometimes the technique of avoidance and reporting of errors in return values does not work. in such cases exception handling techniques can be used.
© The McGraw-Hill Companies, 2006 The Bank constructor revisited a negative value would not be a valid array size, and this would cause an exception in the program; the name of the exception that may be thrown is NegativeArraySizeException. there is no way to report an error has occurred other than to use an exception, as constructors cannot return values (such as booleans) since they have no return type.
© The McGraw-Hill Companies, 2006 Dealing with the problem We could check for this NegativeArraySizeException within the constructor. How would we report back that an error has occurred? usually we would make use of the return value, but constructors have no return value; the only way to report back errors from constructors is to make use of exceptions.
© The McGraw-Hill Companies, 2006 How to inform users of this constructor that an exception may be thrown? Problems with this attempt it is not usually considered good programming practice for methods to claim unchecked exceptions; the underlying implementation (an array) is revealed in the name of the exception. If in future a decision was made to replace the array representation with another representation, this exception name will not be valid.
© The McGraw-Hill Companies, 2006 A second attempt throw a general exception (of type Exception), rather than a specific exception (like NegativeArraySizeIndex for example); in order to throw a general exception object you must use the new command to generate an object of type Exception.
© The McGraw-Hill Companies, 2006 Problems with throwing general exceptions The name of the exception does not explain the source of the problem; the getMessage method must be used to determine that; The catch clause we used to deal with the resulting exception object will catch any exception that is thrown, but we may wish to handle other exceptions in a different way to the constructor exception. Instead of throwing general exceptions, you can throw your own user-defined exceptions.
© The McGraw-Hill Companies, 2006 Creating your own exception classes You can create your own exception class by inheriting from any-predefined exception class. Generally speaking, if you wish your exception class to be unchecked then it should inherit from RuntimeException (or one of its subclasses); If you wish your exception to be checked you can inherit from the general Exception class.
© The McGraw-Hill Companies, 2006 Our own checked NegativeSizException class
© The McGraw-Hill Companies, 2006 Amending the Bank constructor
© The McGraw-Hill Companies, 2006 Re-throwing exceptions ordinarily, when an exception is caught in a catch block, that exception has been dealt with; it is, however, possible to throw an exception from within a catch block. this technique might be useful if the condition required for the if statement were difficult to formulate, or where there were several points in a method where an exception could be thrown.
© The McGraw-Hill Companies, 2006 Documenting exceptions exceptions thrown by methods can be documented using the Javadoc tool; the @throws tag should be used to document the name of any exceptions that may be thrown by a method. here is the Bank constructor, documented with Javadoc comments:
Exceptions By the end of this lecture you should be able to: explain the term exception; distinguish between checked and unchecked exception classes in.
MSc IT Programming Methodology (2). THROWS an EXCEPTION Errors?
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 10: Exceptions Coming up: Exceptions.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 12: Exception Handling. Objectives Learn about exceptions Try code and catch exceptions Throw and catch multiple exceptions Use the finally block.
CIS 270—Application Development II Chapter 13—Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Exception Handling. What are errors? What does exception handling allow us to do? Where are exceptions handled? What does exception handling facilitate?
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Slides Credit Umair Javed LUMS Web Application Development.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 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. Agenda Exception. Handling Exceptions. The finally Clause.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Exception Handling Chapter 12. Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully Natural.
Exceptions CIS 304 Intermediate Java Programming for Business.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions The finally Clause F Cautions.
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.
COMP 121 Week 5: Exceptions and Exception Handling.
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Chapter 8: Exceptions and I/O Streams Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William.
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.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
COMP Exception Handling Yi Hong June 10, 2015.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Copyright © 2012 Pearson Education, Inc. Chapter 11 Exceptions Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2017 SlidePlayer.com Inc. All rights reserved.