java异常类
1 package shb.java.exception; 2 /** 3 * 测试异常类 4 * @Package:shb.java.exception 5 * @Description: 6 * @author shaobn 7 * @Date 2015-9-7上午11:52:42 8 */ 9 public class Demo_2 { 10 public static void main(String[] args) { 11 testTD_5(); 12 } 13 public static void testTD_2(){ 14 TDemo_2 td_2 = new TDemo_2(); 15 try { 16 td_2.getResult(9, 0);//一旦发生异常,函数中后面的方法不再执行即结束 17 System.out.println("over");//发生异常后,此句已不能再执行。 直接抛出异常对象,由catch块中异常对象的引用来接收。 18 } /*catch (Exception e) { 19 // TODO: handle exception 20 System.out.println(e.getMessage()); 21 }*///如果一开始就用Exception会覆盖子异常类,则会只执行Exception,其余都不能执行。 22 catch (ArithmeticException ae) { 23 // TODO: handle exception 24 System.out.println(ae.getMessage()); 25 System.out.println("算术异常"); 26 }catch (ArrayIndexOutOfBoundsException aiobe) { 27 // TODO: handle exception 28 System.out.println(aiobe.getMessage()); 29 System.out.println("数组越界异常"); 30 } 31 System.out.println("helloworld"); 32 } 33 //自定义异常类 34 public static void testTD_3(){ 35 TDemo_3 td_3 = new TDemo_3(); 36 try { 37 td_3.getResult_3(9, 0); 38 System.out.println("over"); 39 } catch (Exception e) { 40 // TODO: handle exception 41 System.out.println(e.getMessage()); 42 } 43 System.out.println("hellobeijing"); 44 } 45 //自定义的继承RuntimeException类的异常(编译时是不报错的,运行时检查异常) 46 public static void testTD_4(){ 47 48 TDemo_4 td_4 = new TDemo_4(); 49 td_4.getResult(9, 0); 50 } 51 public static void testTD_5(){ 52 TDemo_5 td_5 = new TDemo_5(); 53 try { 54 td_5.testMethod(); 55 } catch (Exception e) { 56 // TODO: handle exception 57 System.out.println("异常已经处理"); 58 } 59 } 60 } 61 class TDemo_2{ 62 private int age = 10; 63 public int getAge(){ 64 return this.age; 65 } 66 //声明两个异常,算术异常,数组越界异常。 67 public int getResult(int a,int b) throws ArithmeticException, 68 ArrayIndexOutOfBoundsException{ 69 byte[] arr = new byte[a]; 70 b = a/b; 71 arr[a] = 9; 72 return a/b; 73 } 74 } 75 class TDemo_3{ 76 private int height = 90; 77 public int getHeight(){ 78 return this.height; 79 } 80 public int getResult_3(int a,int b) throws TDemo_3Exception{ 81 if(b==0) 82 //抛出自定义异常,必须在方法上声明异常类。 83 throw new TDemo_3Exception("自定义的异常类"); 84 return a/b; 85 } 86 } 87 //编译时被检测的异常 88 class TDemo_3Exception extends Exception{ 89 public TDemo_3Exception(String str){ 90 super(str); 91 } 92 //重写getMessage()方法 93 public String getMessage(){ 94 return super.getMessage(); 95 } 96 97 } 98 //编译时不被检测的异常 99 class TDemo_4Exception extends RuntimeException{ 100 public TDemo_4Exception(String str){ 101 super(str); 102 } 103 } 104 class TDemo_4{ 105 private int with = 90; 106 public int getWith(){ 107 return this.with; 108 } 109 public int getResult(int a,int b){ 110 if(b==0) 111 throw new TDemo_4Exception("自定义的runtime异常"); 112 return a/b; 113 114 } 115 public void testResult(int a,int b){ 116 if(b<0) 117 throw new TDemo_4Exception("b小于0"); 118 System.out.println("测试成功"); 119 } 120 121 } 122 class TDemo_5Exception extends Exception{ 123 124 public TDemo_5Exception(String str){ 125 super(str); 126 } 127 128 } 129 class TDemo_5{ 130 public void testMethod() throws TDemo_5Exception{ 131 TDemo_4 td_4 = new TDemo_4(); 132 try { 133 td_4.testResult(9, -1); 134 } catch (Exception e) { 135 // TODO: handle exception 136 throw new TDemo_5Exception("处理失败"); 137 } 138 139 } 140 }
吾宁做一叶扁舟,始航于湖边,遨游于海上,浪迹于江中。