I want to redirect only开发者_如何学JAVA stacktrace of uncaught exception from console to log file. The rest of the things should appear on console as usual.
Set a Thread.UncaughtExceptionHandler that prints to the desired file. printStackTrace is threadsafe, so several threads may share the same PrintStream. 
Created a sample program for this, Thanx to gustafc
public class UncaughtException {
    public static void main(String[] args) {        
        Thread.setDefaultUncaughtExceptionHandler( new Thread.UncaughtExceptionHandler(){
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("*****Yeah, Caught the Exception*****");
                e.printStackTrace(); // you can use e.printStackTrace ( printstream ps )
            }
        });     
        System.out.println( 2/0 );  // Throw the Exception 
    }
}
Output is
*****Yeah, Caught the Exception***** java.lang.ArithmeticException: / by zero at thread.UncaughtException.main(UncaughtException.java:12)
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论