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; 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:
CE203 - Application Programming Autumn 2013CE203 Part 21 Part 2.
Introduction Types of Errors Exceptions Exception Handling Common JAVA Exceptions Syntax of Exception Handling Code Example: Divide by Zero.
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.
Error-handling using exceptions Contents: Types of errors Minimizing errors by using control statements and the Debug class Exceptions, Throwing and catching.
ISBN Chapter 14 Exception Handling and Event Handling.
1001ICT Programming 1 Semester 1, 2011 Lecture 8 Course Review 1.
Introduction to Computation and Problem Solving Class 25: Error Handling in Java® Prof. Steven R. Lerman and Dr. V. Judson Harward.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
9.1 9 Programmin g Languages Foundations of Computer Science Cengage Learning.
The. of and a to in is you that it he for.
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
First Program in JAVA: Printing a Line of Text Modifying our First Java Program Escape Sequences Displaying Text with printf Data types in JAVA.
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
COMPILER CONSTRUCTION WEEK- 4: INTRODUCTION TO COMPILER & INTERPRETER.
Lesson 2B – Operations, Characters, Mixing Data Types, Order of Operations By John B. Owen All rights reserved ©2011.
Chapter 5: Distributed systems with JAVA: sockets, RMI, Threads Rufin Soh Wilfried Probst – Rufin Soh INE4481 DISTRIBUTED DATABASES & CLIENT-SERVER ARCHITECTURES.
BD05/06 PL/SQL Introduction Structure of a block Variables and types Accessing the database Control flow Cursors Exceptions Procedures.
Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object.
Unit-V -SOFTWARE QUALITY. To develop and deliver robust system, we need a high level of confidence that Each component will behave correctly Collective.
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
28-May-14 Everything You Ever Wanted To Know About Java O-O.
More on Object-Oriented Programming 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
IOS103 OPERATING SYSTEM VIRTUAL MEMORY. Objectives At the end of the course, the student should be able to: Define virtual memory; Discuss the demand.
MT311 (Oct 2007) Java Application Development Control Structures, Subprograms and Its Implementation Tutorial 7.
Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.
Of. and a to the in is you that it at be.
Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures, Techniques, and Standards.
MT311 (Oct 2006) Java Application Development Exceptions Handling, Streamed I/O, Multithreading and Network Programming Tutorial 3.
© 2016 SlidePlayer.com Inc. All rights reserved.