Discuss / Java / 练习。 同时发现问题:IDEA中正确捕获到了异常,仍然会在控制台提醒代码有异常

练习。 同时发现问题:IDEA中正确捕获到了异常,仍然会在控制台提醒代码有异常

Topic source

张卿长

#1 Created at ... [Delete] [Delete and Lock User]
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()  而是作为日志记录下来。


  • 1

Reply