Like I wrote in my comment, write a try ... catch
block where you write the exception to File
error.txt using Printwriter
.
For instance:
PrintWriter writer = new PrintWriter("error.txt");
try {
//Code to try goes here
} catch (Exception e) {
//You've got an exception. Now print it to error.txt
writer.write(e.toString());
}
You could test with something simple like:
PrintWriter writer = new PrintWriter("error.txt");
int[] i = new int[1];
try {
i[10] = 2;
} catch (Exception e) {
writer.write(e.toString());
}
This results in error.txt being populated with:
java.lang.ArrayIndexOutOfBoundsException: 10
You can print the entire Stack Trace
by doing
e.printStackTrace(writer);
Very important that you don't forget to close the PrintWriter
or your file won't get printed to. Once you are done writing to the file, close with:
writer.close();