This discussion is archived
6 Replies Latest reply: Apr 7, 2010 4:23 PM by EJP RSS

exception handling writeObject method

843790 Newbie
Currently Being Moderated
Hi,

I serialize an object and write it to a file. I want to be sure that if an exception occurs at the method writeObject, i can surely delete the file I created.

          File file;
          ObjectOutputStream output;
          
          file = new File("test.ser");
file.createNewFile();
          output = new ObjectOutputStream(new FileOutputStream(file));

          try {
               output.writeObject(message);
          } catch (IOException e) {
               // ....how to handle
          }

Should I do something like that "file.deleteOnExit()" if file exists, or what else?

Thanks

Cenkbah