Understanding Exception Handling in Intermediate Java

Slide Note
Embed
Share

Exception handling is crucial in Java programming to manage errors and unexpected events that can disrupt program execution. This article covers the basics of exceptions, throwing and handling exceptions, types of exceptions (checked and unchecked), and use of try, catch, and finally blocks. Learn how to effectively deal with errors and ensure the smooth flow of your Java programs.


Uploaded on Oct 04, 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. COP3804 - INTERMEDIATE JAVA Exception Handling Serialization

  2. Exceptions An exception is an object that is generated as the result of an error or an unexpected event, which occurs during the execution of a program, and that disrupts the normal flow of a program's instructions.

  3. Exceptions Throwing an exception: When an error occurs within a method, the method creates an exception object and hands it off to the runtime system. Handling an exception: When the runtime system receives an exception object, it tries to find code that can handle it, beginning with the method in which the error occurred and proceeds, in reverse order, with all the methods that were called to get to the method that threw the error (the call stack). If the code does not handle an exception when it is thrown, the default exception handler deals with it. It prints an error message and crashes the program.

  4. Exception Handling The call stack: The list of methods that were called to get to the method where the error occurred. Exception handler: The block of code that can handle an exception.

  5. Unchecked Exceptions Unchecked exceptions are those that inherit from the Error class (thrown when a critical error occurs) or the RuntimeException class (programming errors). Exceptions that inherit from the RuntimeException class can be prevented, they are a fault in the code. i.e. NullPointerException

  6. Checked Exceptions All exceptions that do not inherit from the Error or the RuntimeException classes are checked exceptions. Checked exceptions are due to external circumstances that the programmer cannot prevent. The compiler checks that your programs handle these exceptions by either providing exception handler code or by having a throws clause. The application should anticipate and recover from checked exceptions. i.e. Trying to open a file using a bad file name

  7. Exceptions

  8. Try, Catch and Finally blocks The try block identifies a block of code in which an exception can occur. The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception. The finally block is a block of code that is guaranteed to execute, and is the right place to recover resources. The try statement should contain at least one catch block or a finally block and may have multiple catch blocks. If an exception occurs within a try block, it is handled by the catch block (exception handler) following the try block that handles that specific type of exception.

  9. Serialization The process of saving objects to a file. When an object is serialized, it is converted into a series of bytes that contain the object s data. The resulting set of bytes can then be saved to a file for later retrieval. In order for an object to be serialized, its class must implement the Serializable interface. This interface has no methods or fields, it only tells Java that the objects of the class may be serialized.

  10. Serialization If a class implements the Serializable interface, then all of the fields in that class must be serializable. The transient keyword can be used to indicate that a field is skipped during the serialization process. The following classes are used during serialization: java.io.ObjectInputStream java.io.FileInputStream java.io.ObjectOutputStream convert an object into a series of bytes java.io.FileOutputStream write the bytes into a file convert a series of bytes into an object read the bytes from a file together with the corresponding readObject and writeObject methods.

  11. References Horstmann, Cay. Big Java 4th ed. New York, USA: John Wiley & Sons, Inc., 2010. Oracle. The Java Tutorials, 2013. Web. 25 Aug. 2013. http://docs.oracle.com/javase/tutorial/index.html Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2nd ed. Boston, USA: Addison-Wesley, 2012

Related


More Related Content