I want to print the stack (after an error in an exception block, try catch
), but I wanna print all in an external file (.txt), file that I can take somewhere in the android folders.
Thanks in advance
I want to print the stack (after an error in an exception block, try catch
), but I wanna print all in an external file (.txt), file that I can take somewhere in the android folders.
Thanks in advance
Open/declare a PrintWriter
to write to your file and change your catch block to:
catch(Exception e) {
e.printStackTrace(your_print_writer);
}
Well, that could be a problem, but it's totally doable:
try {
badMethod();
} catch(SomeException e) {
try (FileWriter writer = new FileWriter("file.txt")) {
e.printStackTrace(writer);
} catch(IOException ioexception) {
// do something here
}
}
There are a lot of ways to do that, but maybe the easiest is to redirect System.err to your desired file an then simply call Exception.printStackTrace();
To redirect standard error output:
PrintStream ps = new PrintStream("./errors.txt");
System.setErr(ps);
and then...
catch(Exception e) {
e.printStackTrace();
}
You can do it this way :
try{
// action
} catch (IOException e) {
PrintWriter writer = new PrintWriter("stacktrace.txt");
e.printStackTrace(writer);
writer.close();
}