异常处理(Exception)

异常处理相关基础操作~~~

(1)异常处理方法

(2)多个异常处理

(3)Finally的用法

(4)使用catch处理异常

(5)多线程处理异常

(6)获取异常的堆栈信息

(7)重载方法处理异常

(8)链试异常

(9)自定义异常

  1 public class ExceptionControl {
  2     // 异常处理方法
  3     private static void ExceptionFunction() {
  4         try {
  5             throw new Exception("MY Exception");
  6         } catch (Exception e) {
  7             System.err.println("Caught Exception");
  8             System.err.println("getMessage():" + e.getMessage());
  9             System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());
 10             System.err.println("toString():" + e);
 11             System.err.println("printStackTrace():");
 12             e.printStackTrace();
 13         }
 14     }
 15 
 16 
 17     // 多个异常处理
 18     static class Demo {
 19         int div(int a, int b) throws ArithmeticException, ArrayIndexOutOfBoundsException {
 20             int[] arr = new int[a];
 21             System.out.println(arr[4]);     //制造的第一处异常
 22             return a / b;     //制造的第二处异常
 23         }
 24     }
 25 
 26     private static void ManyCatch() {
 27         Demo d = new Demo();
 28         try {
 29             int x = d.div(4, 0);
 30             //int x = d.div(5,0);
 31             //int x = d.div(4,1);
 32             System.out.println("x=" + x);
 33         } catch (ArithmeticException e) {
 34             System.out.println(e.toString());
 35         } catch (ArrayIndexOutOfBoundsException e) {
 36             System.out.println(e.toString());
 37         } catch (Exception e) {
 38             System.out.println(e.toString());
 39         }
 40         System.out.println("Over");
 41     }
 42 
 43     // Finally的用法
 44     private static void FinallyUse() {
 45         new ExceptionDemo2().doTheWork();
 46     }
 47 
 48     static class ExceptionDemo2 {
 49         public static void doTheWork() {
 50             Object o = null;
 51             for (int i = 0; i < 5; i++) {
 52                 try {
 53                     o = makeObj(i);
 54                 } catch (IllegalArgumentException e) {
 55                     System.err.println
 56                             ("Error: (" + e.getMessage() + ").");
 57                     return;
 58                 } finally {
 59                     System.err.println("都已执行完毕");
 60                     if (o == null)
 61                         System.exit(0);
 62                 }
 63                 System.out.println(o);
 64             }
 65         }
 66 
 67         private static Object makeObj(int type)
 68                 throws IllegalArgumentException {
 69             if (type == 1)
 70                 throw new IllegalArgumentException
 71                         ("不是指定的类型: " + type);
 72             return new Object();
 73         }
 74     }
 75 
 76 
 77     // 使用catch处理异常
 78     private static void UseCatchDealException() {
 79         int array[] = {20, 20, 40};
 80         int num1 = 15, num2 = 10;
 81         int result = 5;
 82         try {
 83             result = num1 / num2;
 84             System.out.println("结果为 " + result);
 85             for (int i = 5; i >= 0; i--) {
 86                 System.out.println("数组的元素值为 " + array[i]);
 87             }
 88         } catch (Exception e) {
 89             System.out.println("触发异常 : " + e);
 90         }
 91     }
 92 
 93 
 94     // 多线程处理异常
 95     private static void ThreadingException() {
 96         MyThread t = new MyThread();
 97         t.start();
 98         try {
 99             Thread.sleep(1000);
100         } catch (Exception x) {
101             System.out.println("Caught it" + x);
102         }
103         System.out.println("Exiting main");
104     }
105 
106     static class MyThread extends Thread {
107         public void run() {
108             System.out.println("Throwing in " + "MyThread");
109             throw new RuntimeException();
110         }
111     }
112 
113 
114     // 获取异常的堆栈信息
115     private static void GetExceptionStackInfo() {
116         int array[] = {20, 20, 40};
117         int num1 = 15, num2 = 10;
118         int result = 10;
119         try {
120             result = num1 / num2;
121             System.out.println("The result is" + result);
122             for (int i = 5; i <= 0; i--) {
123                 System.out.println("The value of array is" + array[i]);
124             }
125         } catch (Exception e) {
126             e.printStackTrace();
127         }
128     }
129 
130 
131     // 重载方法异常处理
132     private static void LoadFunctionException() {
133         MainDemo mn = new MainDemo();
134         try {
135             System.out.println(mn.method(10, 20.0));
136             System.out.println(mn.method(10.0, 20));
137             System.out.println(mn.method(10.0, 20.0));
138             System.out.println(mn.method(10));
139         } catch (Exception ex) {
140             System.out.println("exception occoure: " + ex);
141         }
142     }
143 
144     static class MainDemo {
145         double method(int i) throws Exception {
146             return i / 0;
147         }
148 
149         static boolean method(boolean b) {
150             return !b;
151         }
152 
153         static double method(int x, double y) throws Exception {
154             return x + y;
155         }
156 
157         static double method(double x, double y) {
158             return x + y - 3;
159         }
160     }
161 
162 
163     // 链试异常
164     private static void DealLinkException() throws Exception {
165         int n = 20, result = 0;
166         try {
167             result = n / 0;
168             System.out.println("结果为" + result);
169         } catch (ArithmeticException ex) {
170             System.out.println("发算术异常: " + ex);
171             try {
172                 throw new NumberFormatException();
173             } catch (NumberFormatException ex1) {
174                 System.out.println("手动抛出链试异常 : " + ex1);
175             }
176         }
177     }
178 
179 
180     // 自定义异常
181     private static void DelDiyException() {
182         try {
183             new Input().method();
184         } catch (WrongInputException wie) {
185             System.out.println(wie.getMessage());
186         }
187     }
188 
189 
190     static class WrongInputException extends Exception {
191         WrongInputException(String s) {
192             super(s);
193         }
194     }
195 
196     static class Input {
197         void method() throws WrongInputException {
198             throw new WrongInputException("Wrong input");
199         }
200     }
201 
202     public static void main(String[] args) throws Exception {
203         ExceptionFunction();
204         ManyCatch();
205         FinallyUse();
206         UseCatchDealException();
207         ThreadingException();
208         GetExceptionStackInfo();
209         LoadFunctionException();
210         DealLinkException();
211         DelDiyException();
212     }
213 
214 }

猜你喜欢

转载自www.cnblogs.com/skygrass0531/p/12311871.html
今日推荐