1 package yichangchuli;
2
3 import java.io.IOException;
4
5 public class ttxt {
6
7 public static void main(String[] args) {
8 //捕获异常
9 //抛出异常 throw 写在方法里面的 throws 写在方法名的后面
10 try
11 {
12
13 int in =0;
14 if (in ==0)
15 {
16 Exception ex = new Exception ("输出的数字为0");
17
18 throw ex;
19 }
20
21
22 int r = 3 / in;
23 //
24 System.out.println("结果=" +r);
25
26
27 myException me = new myException ("我自定义的异常类");
28
29 throw me;
30
31 }
32 catch(IOException e)
33 {
34
35 }
36 catch (Exception e)
37 {
38 //处理
39 // 1 :记录日志、文件或数据库
40 // 2 :输出友好的提示信息
41 // 3 :执行的特别代码
42
43 //Exception 在 java 里表示异常的类,是所有异常的父类
44 System.out.println("异常信息="+e.getMessage()); //获取异常信息
45 e.printStackTrace(); //向控制台输出异常
46
47
48
49 }
50 finally //一定会被执行的
51 {
52 System.out.println("痴汉电车");
53 //System.exit(0);
54 }
55
56
57
58 }
59
60 }
61
62
63
64
65
66
67 package yichangchuli;
68
69 public class myException extends Exception {
70
71 public myException(String mess)
72 {
73 super(mess);
74 }
75
76
77
78
79 }