程序自己不处理异常,而把它抛给调用者,让它去处理。
package testexception; public class Testthrow { static int a; static int b=4; static int c; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub test(b,c); } public static void test(int b,int c)throws Exception{ if(c==0){ throw new ArithmeticException(); } a=b/c; System.out.println(a); } }打印结果:
package testexception; public class Testthrow { static int a; static int b=4; static int c=1; static int[] array; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub test(b,c,null); } public static void test(int b,int c,int[]array)throws Exception{ if(c==0||array==null){ throw new ArithmeticException(); } a=b/c; System.out.println(a); } }打印结果:
package testexception; public class Testthrow { static int a; static int b=4; static int c=1; static int[] array; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub test(b,c,null); } public static void test(int b,int c,int[]array)throws Exception{ if(c==0){ throw new ArithmeticException(); } if(array==null){ throw new NullPointerException(); } a=b/c; System.out.println(a); } }打印结果如下:Exception in thread "main" java.lang.NullPointerException