I need to capture the exception in a text file in Java. For example:
try {
File f = new File(\"\");
}
catch(FileNotFoundException f) {
f.printStackTrac
Hope below example helps you-
package com.kodehelp.javaio;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* Created by https://kodehelp.com
* Date: 03/05/2012
*/
public class PrintStackTraceToFile {
public static void main(String[] args) {
PrintStream ps= null;
try {
ps = new PrintStream(new File("/sample.log"));
throw new FileNotFoundException("Sample Exception");
} catch (FileNotFoundException e) {
e.printStackTrace(ps);
}
}
}
For more detail, refer to this link here