I am trying to print my stack trace on a file. So far I am successful. But, when I try to create the log file name using current time and date, the main program throws exception in the Output(NetBeans) window instead allowing my code to handle it.

I am using the following code :

package SerializableTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main{
    PrintStream printStream;

    public static void main(String... args) {
        Main app = new Main();
        app.createLogger();
        app.start();
    }

    private void start() {
        Node testNode = new Node(1f, 2f);
        
        try {
            FileOutputStream fileInputStream = new FileOutputStream("serializable.ser");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileInputStream);
            objectOutputStream.writeObject(testNode);
            objectOutputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace(printStream);            
        }

    }

    private void createLogger() {        
        try{
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH.mm.ss");
            String fileName = "Crash " + dateFormat.format(Calendar.getInstance().getTime()) + ".log";                        
            FileOutputStream loggerStream = new FileOutputStream(fileName);
//            FileOutputStream loggerStream = new FileOutputStream("Report.log");
            printStream = new PrintStream(loggerStream);  
        } catch(Exception ex){
        }
    }
}

i want to use this code, but the exception its thrown before my program runs.

If i comment out line 36,37 & 38 and un-comment line 39 then program runs fine, and exception is handled and reported by my program. Why using these three lines makes the program work in a different way : Exception is handled before the program runs, and its the following,

Exception in thread "main" java.lang.NullPointerException
	at java.lang.Throwable.printStackTrace(Throwable.java:460)
	at SerializableTest.Main.start(Main.java:30)
	at SerializableTest.Main.main(Main.java:18)
Java Result: 1

Edited 5 Years Ago by iamcreasy: n/a

The problem was solved after changing the line 37 as following,

DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH.mm.ss");

That stopped the E being thrown, but your other problem is on line 30 where printstream is null if a E is thrown before printstream is fully initialised or the initialisation fails - you have committed a major error on lines 42/43 by "eating" the Exception without any kind of diagnostics - an empty catch clause is never a good idea when developing code.

Edited 5 Years Ago by JamesCherrill: Never have an empthy catch clause

This article has been dead for over six months. Start a new discussion instead.