class Demo { int div(int a,int b)throws Exception //在功能上通过throws的关键字声明了该功能有可能会出现问题 { return a/b; } } public class ExceptionDemo { public static void main(String[] args)//throws Exception { Demo d=new Demo(); //int x=d.div(4, 1); //System.out.println("x="+x); try { int x=d.div(4, 1); System.out.println("x="+x); } catch(Exception e) { System.out.println("除零啦"); System.out.println(e.getMessage()); // /by zero System.out.println(e.toString());//异常名称:异常信息 e.printStackTrace();//异常名称,异常信息,异常出现的位置。 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息。 } } }
4.对多异常的处理。
1,声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
2,对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。
如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
建立在进行catch处理时,catch中一定要定义具体处理方式。
不要简单定义一句 e.printStackTrace(),
也不要简单的就书写一条输出语句。
class Demo { int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException //在功能上通过throws的关键字声明了该功能有可能会出现问题 { int[] arr=new int [a]; System.out.println(arr[4]); return a/b; } } public class ExceptionDemo { public static void main(String[] args) //throws Exception { Demo d=new Demo(); //int x=d.div(4, 1); //System.out.println("x="+x); try { int x=d.div(2, 0); System.out.println("x="+x); } catch(ArithmeticException e) { System.out.println("除零啦"); e.printStackTrace();//异常名称,异常信息,异常出现的位置。 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息。 } catch(ArrayIndexOutOfBoundsException e) { System.out.println("越界啦"); e.printStackTrace();//异常名称,异常信息,异常出现的位置。 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息。 } //System.out.println("over"); } } 运行结果: 越界啦 java.lang.ArrayIndexOutOfBoundsException: 4 at Demo.div(ExceptionDemo.java:6) at ExceptionDemo.main(ExceptionDemo.java:23)
class FuShuException extends Exception { private int value; FuShuException() { super(); } FuShuException(String msg,int value) { super(msg); this.value=value; } public int getValue() { return value; } } class Demo { int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException,FuShuException //在功能上通过throws的关键字声明了该功能有可能会出现问题 { if(b<0) //throw new FuShuException() ; throw new FuShuException("出现了除数是负数的情况 ----/by fushu",b) ;//手动通过throw关键字抛出一个自定义异常 return a/b; } } public class ExceptionDemo { public static void main(String[] args) //throws Exception { Demo d=new Demo(); //int x=d.div(4, 1); //System.out.println("x="+x); try { int x=d.div(3, -1); System.out.println("x="+x); } catch(ArithmeticException e) { System.out.println("除零啦"); e.printStackTrace();//异常名称,异常信息,异常出现的位置。 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息。 } catch(ArrayIndexOutOfBoundsException e) { System.out.println("越界啦"); e.printStackTrace();//异常名称,异常信息,异常出现的位置。 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息。 } catch(FuShuException e) { //System.out.println("除数是负数"); e.printStackTrace();//异常名称,异常信息,异常出现的位置。 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息。 System.out.println("除数是负数:"+e.getValue()); } //System.out.println("over"); } }
class FuShuException extends RuntimeException { FuShuException(String msg) { super(msg); } } class Demo { int div(int a,int b)//throws Exception//throws ArithmeticException { if(b<0) throw new FuShuException("出现了除数为负数了"); //if(b==0) //throw new ArithmeticException("被零除啦"); return a/b; } } public class ExceptionDemo { public static void main(String[] args) //throws Exception { Demo d=new Demo(); int x=d.div(4, -1); System.out.println("x="+x); } }
原文:http://www.cnblogs.com/shihuai355/p/3806300.html