码迷,mamicode.com
首页 > 编程语言 > 详细

Java异常处理

时间:2016-02-03 23:31:10      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:

异常的层次结构
  所有的异常类都是 java.lang.Exception 类的子类型。异常类都是 Throwable 类的子类。除了异常类 Error 类也是由 Throwable 类产生的的子类
1. public String getMessage()
返回关于发生异常的细节信息,这些信息在Throwable的构造函数中被初始化
2. public Throwable getCause()
返回发生异常的原因,由 Throwable 对象来表示
3. public String toString()
返回与getMessage()的结果相联系的类的名称
4. public void printStackTrace()
打印 toString()跟踪错误输出流的栈地址的结果
5. public StackTraceElement [] getStackTrace()
返回一个数组,其中包含每个元素在栈的地址,元素的索引0代表调用栈的顶部,最后一个元素表示方法调用栈的底部
6. public Throwable fillInStackTrace()
用当前栈地址来填充 Throwable 对象的栈地址,添加到任何先前的栈地址信息

 1 package Test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class test1 {
 6     public static void main(String[] args) throws InsufficientFundsException{
 7         Scanner input = new Scanner(System.in);
 8         System.out.println("Enter two integers: ");
 9         int number1 = input.nextInt();
10         int number2 = input.nextInt();
11         //1.
12 //        if(number2 != 0)
13 //            System.out.println(number1 + "/" + number2 + " is " + (number1 / number2));
14 //        else
15 //            System.out.println("你好");
16         //2.
17         try{
18             System.out.println(number1 + "/" + number2 + " is " + (number1 / number2));
19         }
20         //catch 关键字表示可被捕获的异常
21         catch(Exception e){
22             System.out.println("程序存在异常:"+e);
23         }finally {
24             System.out.println("结束");
25         }
26         //3.
27         if(number2 != 0){
28             System.out.println(number1 + "/" + number2 + " is " + (number1 / number2));
29         }else{
30             //抛出异常
31             throw new ArithmeticException("错了");
32             
33 //            ArithmeticException ex = new ArithmeticException("OK~");
34 //            System.out.println(ex.getMessage());
35 //            //throw 关键字,称为抛出一个异常
36 //            throw ex;
37         }
38         //4.自定义异常处理
39         try{
40             System.out.println("开始");
41             check_test();
42         }catch(InsufficientFundsException e){
43             System.out.println(e.getScore()+"-"+e);
44         }finally{
45             System.out.println("结束");
46         }
47     }
48     public static void check_test() throws InsufficientFundsException{
49         double ac = 1.1;
50         System.out.println("check_test()");
51         throw new InsufficientFundsException(ac);
52     }
53 }

 

技术分享
 1 package Test;
 2 
 3 public class InsufficientFundsException extends Exception{
 4     
 5     private double score;
 6     /**
 7      * 自定义异常处理方法
 8      * @param score
 9      */
10     public InsufficientFundsException(double score) {
11         super("Hello SuperMan");
12         this.score = score;
13     }
14     
15     public double getScore(){
16         return score;
17     }
18 }
InsufficientFundsException.java

 

Java异常处理

标签:

原文地址:http://www.cnblogs.com/zhengbin/p/5180770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!