Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP3804 - INTERMEDIATE JAVA Exception Handling Serialization.

Similar presentations

Presentation on theme: "COP3804 - INTERMEDIATE JAVA Exception Handling Serialization."— 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: 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. Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2 nd ed. Boston, USA: Addison-Wesley, 2012

Download ppt "COP3804 - INTERMEDIATE JAVA Exception Handling Serialization."

Similar presentations

Ads by Google