Understanding Exception Handling in Object-Oriented Programming

Slide Note
Embed
Share

Exception handling in object-oriented programming enables a program to manage and recover from exceptional situations during runtime errors. Java uses exceptions to represent errors, allowing methods to throw exceptions that can be caught and handled by the caller, thus separating error detection and handling. Different types of exceptions can be utilized, including system errors, exceptions, and runtime exceptions, with the ability to create custom exception classes.


Uploaded on Nov 23, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Exception Handling Object Oriented Programming 1

  2. What is it? Exception handling enables a program to deal with exceptional situations and continue its normal execution. Runtime errors occur while a program is running if the JVM detects an operation that is impossible to carry out. For example, if you access an array using an index that is out of bounds, you will get a runtime error with an ArrayIndexOutOfBoundsException. If you enter a double value when your program expects an integer, you will get a runtime error with an InputMismatchException. Object Oriented Programming 2

  3. Cont. In Java, runtime errors are thrown as exceptions. An exception is an object that represents an error or a condition that prevents execution from proceeding normally. If the exception is not handled, the program will terminate abnormally. How can you handle the exception so that the program can continue to run or else terminate gracefully? QuotientWithMethod.java Object Oriented Programming 3

  4. Cont. This is clearly a problem. You should not let the method terminate the program the caller should decide whether to terminate the program. How can a method notify its caller an exception has occurred? Java enables a method to throw an exception that can be caught and handled by the caller. QuotientWithException.java Object Oriented Programming 4

  5. Cont. It enables a method to throw an exception to its caller, enabling the caller to handle the exception. Without this capability, the called method itself must handle the exception or terminate the program. Often the called method does not know what to do in case of error. This is typically the case for the library methods. The library method can detect the error, but only the caller knows what needs to be done when an error occurs. The key benefit of exception handling is separating the detection of an error (done in a called method) from the handling of an error (done in the calling method). Many library methods throw exceptions. An example that handles an InputMismatchException when reading an input. InputMismatchExceptionDemo.java Object Oriented Programming 5

  6. Exception Types Exceptions are objects, and objects are defined using classes. The root class for exceptions is java.lang.Throwable. Are there any other types of exceptions you can use? Can you define your own exception classes? Yes. There are many predefined exception classes in the Java API. Object Oriented Programming 6

  7. Cont. The exception classes can be classified into three major types: system errors, exceptions, and runtime exceptions. System errors are thrown by the JVM and are represented in the Error class. The Error class describes internal system errors, though such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. Object Oriented Programming 7

  8. Cont. Runtime exceptions are represented in the RuntimeException describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the JVM. class, which Object Oriented Programming 8

  9. Cont. Exceptions Exception class, which describes errors caused by your program and by external circumstances. These errors can be caught and handled by your program. are represented in the Object Oriented Programming 9

  10. Cont. Java s exception-handling model is based on three operations: declaring an exception, throwing an exception, and catching an exception. A handler for an exception is found by propagating the exception backward through a chain of method calls, starting from the current method Object Oriented Programming 10

  11. TestCircleWithRadiusException.java Object Oriented Programming 11

  12. Finally clause Occasionally, you may want some code to be executed regardless of whether an exception occurs or is caught. Java has a finally clause that can be used to accomplish this objective. Object Oriented Programming 12

  13. Cont. Consider three possible cases: If no finalStatements is executed, and the next statement after the try statement is executed. If a statement causes an exception in the try block that is caught in a catch block, the rest of the statements in the try block are skipped, the catch block is executed, and the finally clause is executed. The next statement after the try statement is executed. If one of the statements causes an exception that is not caught in any catch block, the other statements in the try block are skipped, the finally clause is executed, and the exception is passed to the caller of this method. Note: The finally block executes even if there is a return statement prior to reaching the finally block exception arises in the try block, Object Oriented Programming 13

  14. Getting Information from Exceptions An exception object contains valuable information about the exception. You may use the following instance methods in the java.lang.Throwable class to get information regarding the exception. TestException.java Object Oriented Programming 14

  15. When to use it? Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources, because it requires instantiating a new exception object, rolling back the call stack, and propagating the exception through the chain of methods invoked to search for the handler. When should you use a try-catch block in the code? Use it when you have to deal with unexpected error conditions. Do not use a try-catch block to deal with simple, expected situations. Object Oriented Programming 15

  16. Defining Custom Exception class Java provide quite a few exceptions classes. If you run into a problem that cannot be adequately described by the predefined exception classes, you can create your own exception class, derived from Exception or from a subclass of Exception, such as IOException TestCircleWithCustomException.java Object Oriented Programming 16

  17. Re throwing exceptions Java allows an exception handler to rethrow the exception if the handler cannot process the exception or simply wants to let its caller be notified of the exception. The statement throw ex rethrows the exception to the caller so that other handlers in the caller get a chance to process the exception ex. Object Oriented Programming 17

  18. Chained exceptions Throwing an exception along with another exception forms a chained exception. ChainedExceptionDemo.java Object Oriented Programming 18

  19. File Class The File class contains the methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory. To store output permanently and to read data from it. Absolute path Vs Relative path TestFileClass.java Object Oriented Programming 19

  20. File input and output Use the Scanner class for reading text data from a file and the PrintWriter class for writing text data to a file. WriteData.java Programmers often forget to close the file. - WriteDataWithAutoClose.java Reading data from file using Scanner class ReadData.java Object Oriented Programming 20

  21. Reading data from web Just like you can read data from a file on your computer, you can read data from a file on the Web. ReadFileFromURL.java Program of Web Crawler Object Oriented Programming 21

Related


More Related Content