The document discusses Java serialization and exceptions. It provides examples of implementing serialization by having a class implement the Serializable interface, which allows object instances to be written to and read from files. It also discusses exception handling using try-catch blocks and the finally clause, and how exceptions can be declared to be thrown from a method using the throws clause.
2. What is serialization?
Example: we want to store an object on the
disk and recover later
public class SerializableDemo implements Serializable {
Integer id;
String name;
String address;
public static void main(String[] args) {
SerializableDemo s = new SerializableDemo();
s.id= new Integer(9);
s.name = "John";
s.address = "34 Road, Dhanmondi";
//writing
File f = new File("/home/user/eu/oop/SerializableDemo.ser");
3. try{ Rest of the code
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(f));
o.writeObject(s);
o.close();
}catch(Exception e){
e.printStackTrace();
System.exit(0);
}
//reading
try{
ObjectInputStream i = new ObjectInputStream(new FileInputStream(f));
SerializableDemo d = (SerializableDemo)i.readObject();
i.close();
System.out.println(d.id);
System.out.println(d.name);
System.out.println(d.address);
}catch(Exception e){
e.printStackTrace();
}
}
}
4. Output
9
John
34 Road, Dhanmondi
Lesson learned: we can save an object and retrieve if later if
implement java.io.Serializable interface
5. Another serializable demo
public class SerializableDemo2 extends JFrame{
SerializableDemo2(){
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new JButton("Test button"));
}
public static void main(String[] args) {
SerializableDemo2 s = new SerializableDemo2();
s.setVisible(true);
//writing
File f = new File("/home/user/eu/oop/SerializableDemo2.ser");
try{
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(f));
o.writeObject(s);
o.close();
}catch(Exception e){
e.printStackTrace();
System.exit(0);
}
}
}
7. We want to read the serialized
object by another program
//reading
File f = new File("/home/user/eu/oop/SerializableDemo2.ser");
try{
ObjectInputStream i = new ObjectInputStream(new FileInputStream(f));
SerializableDemo2 d = (SerializableDemo2)i.readObject();
d.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
The same frame shows after
reconsructing from serialized
object
8. Exception handling
try-catch-finally
public class TryCatchDemo {
public static void main(String[] args) {
File file = new File("/home/user/tmp");
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
}catch(FileNotFoundException e){
System.out.println("The file you are trying to access is not
available");
}finally{
try{
fis.close();
}catch(Exception ex){
//nothing to do
}
}
}
}
9. Details about exceptions
At first code in the try clause is executed
If there is any exception code in Exception
clause is executed
Finally clause is executed after the try clause
is executed. We close any resources in the
finally clause
Finally clause is not mandatory
Exceptions can be extended just time any
other java class
10. More on exceptions
If some code in a method raises some
exception we must handle it either by
wrapping the code with try-catch block
Alternatively, you can re-raise the exception by
using throws clause in the method
public void doSomething(){
File file = new File("/home/user/a.out");
FileInputStream fis = null;
fis = new FileInputStream(file);
}
//this code will not compile
11. throws clause
This will compile; but the caller must handle
the excepion or declare its own throws
public void doSomething() throws FileNotFoundException{
File file = new File("/home/user/a.out");
FileInputStream fis = null;
fis = new FileInputStream(file);
}