---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
1 package blogtest4; 2 /* 3 * 自定义异常类 4 */ 5 class EceptionTest1 6 { 7 public static void main(String args[]) 8 { 9 Demo demo = new Demo(); 10 try 11 { 12 demo.div(1,-1,3); 13 } 14 catch(ChuShuException e) 15 { 16 System.out.println(e.toString()); 17 } 18 19 catch (ArithmeticException e) 20 { 21 System.out.println(e.toString()); 22 } 23 catch (ArrayIndexOutOfBoundsException e) 24 { 25 System.out.println(e.toString()); 26 } 27 } 28 } 29 class Demo 30 { 31 public int div(int a,int b,int c)throws ChuShuException,ArithmeticException,ArrayIndexOutOfBoundsException 32 { 33 int[] arr = new int[]{1,2,3,4}; 34 System.out.println(arr[c]); 35 if(b<0) 36 throw new ChuShuException("chushu wei fushu"); 37 return a/b; 38 } 39 } 40 class ChuShuException extends Exception 41 { 42 ChuShuException(String mes) 43 { 44 super(mes); 45 } 46 }
1 练习 2 package blogtest4; 3 public class EceptionTest2 { 4 /** 5 * 自定义RuntimeExcption子类异常 6 */ 7 public static void main(String[] args) { 8 new RuntimeTest().ChuShu(4,-1); 9 } 10 } 11 class RuntimeExceptionTest extends RuntimeException 12 { 13 RuntimeExceptionTest(String a) 14 { 15 super(a); 16 } 17 18 } 19 class RuntimeTest 20 { 21 int ChuShu(int a,int b) 22 { 23 if(b<0) 24 throw new RuntimeExceptionTest("xiao yu ling"); 25 return a/b; 26 } 27 }
原文:http://www.cnblogs.com/yuemingxingxing/p/5077833.html