java ---异常处理

1.使用throws关键字放在方法名括号的外面,修饰改方法,表示 抛出的异常给调用该方法的方法处理,也就是说谁调用谁负责。

2.使用throw关键字放在try{}catch(exection e){}的try里面,表示抛出的异常由本方法自己处理。若没有放在try{}块中,则需要加上throws关键字抛出该异常由调用这个方法的方法处理。

测试程序:

 1 package com;
2
3 public class ExTest {
4
5 public static void main(String[] args){
6 ExTest t = new ExTest();
7 int flag = t.testThr();
8 System.out.println("main= "+flag);
9 }
10 public int testThr(){
11 int b = 0;
12 try {
13 b = threxc();
14 } catch (Exception e) {
15 System.out.println("testThr: "+e.getMessage());
16 e.printStackTrace();
17 }
18 return b;
19 }
20 public int threxc() throws Exception
21 {
22 try{
23 int a = 4/0;
24 return a;
25 }catch (Exception e) {
26 System.out.println("threxc: "+e.getMessage());
27 throw e;
28
29 }
30 //return 1;
31 }
32 }



posted @ 2012-04-06 10:14  Panda.Xiong  阅读(170)  评论(0编辑  收藏  举报