public class CatchExceptionDemo { public static void main(String[] args) { String a="8"; String b="sadf"; try { StringToInt(a); StringToInt(b); } catch (NumberFormatException e){ System.out.println("In NumberFormatException"); e.printStackTrace(); } catch(Exception e){ System.out.println("In Exception"); e.printStackTrace(); } finally { System.out.println("end"); } } static int StringToInt(String s){ return Integer.parseInt(s); }}
即便这里正确地捕获到了异常——NumberFormatException ,但是由于我在其中调用了e.printStackTrace();他会将异常的堆栈跟踪打印到控制台;所以出现了如题描述的情况。一般开发中不会直接调用printStackTrace() 而是作为日志记录下来。
Sign in to make a reply
张卿长
即便这里正确地捕获到了异常——NumberFormatException ,但是由于我在其中调用了e.printStackTrace();他会将异常的堆栈跟踪打印到控制台;所以出现了如题描述的情况。一般开发中不会直接调用printStackTrace() 而是作为日志记录下来。