- Redirect PrintStackTrace () to a string

zhaozj2021-02-08  460

import java.io. *;. public class TestStack2String {public static void main (String s []) {try {// force an exception for demonstration purpose Class.forName ( "unknown") newInstance ();} catch (Exception e ) {System.out.println (stack2string (e));}} static public String stack2string (Exception e) {try {StringWriter sw = new StringWriter (); PrintWriter pw = new PrintWriter (sw); e.printStackTrace (pw) RETURN "------ / r / n" sw.toString () "------ / r / n";} catch (Exception E2) {Return "Bad stack2string";}}}

转载请注明原文地址:https://www.9cbs.com/read-895.html

New Post(0)