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.
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.
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.
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
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
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.
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.
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.ObjectOutputStream together with the corresponding readObject and writeObject methods.
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 2 nd ed. Boston, USA: Addison-Wesley, 2012