1 package bao1;
2
3 public class Text2
4 {
5 public int chufa(int a,int b)throws Exception//声明语句,语句里面可能会出现错误
6 {
7 if(b==0)
8 {
9 throw new Exception("参数b不能为0");
10 }
11 return a/b;
12 }
13 //text()这个方法执行了上一个方法
14 public void text() throws Exception//也要声明语句可能会有错误
15 {
16 chufa(6,2);
17 }
18 }
1 package bao1;
2
3 public class TextText2
4 {
5
6 public static void main(String[] args)
7 {
8 Text2 a1=new Text2();
9 try
10 {
11 a1.chufa(12, 0);
12 }
13 catch (Exception ex)
14 {
15 ex.printStackTrace();
16 }
17
18 try
19 {
20 a1.text();
21 }
22 catch (Exception ex)
23 {
24 ex.printStackTrace();
25 }
26
27 }
28
29 }