zoukankan      html  css  js  c++  java
  • Java基础——异常处理

    异常的层次结构
      所有的异常类都是 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 对象的栈地址,添加到任何先前的栈地址信息

      即使在到达finally块之前有一个return语句,finally块还是会执行。

     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

     http://www.cnblogs.com/focusj/archive/2011/12/26/2301524.html

  • 相关阅读:
    外观模式
    装饰器模式
    eclipse在运行main方法时在console里面报内存溢出的错误解决办法
    windows7安装node
    Eclipse-低版本离线集成svn步骤
    IDEA-JetBrains产品永久破解
    Java对字符串使用MD5进行加密(亲测有效)
    windows下的java项目打jar分别编写在windows与linux下运行的脚本( 本人亲测可用!)
    在linux中运行main方法所在的java类(亲测有效!!!)
    linux常用命令
  • 原文地址:https://www.cnblogs.com/zhengbin/p/5180770.html
Copyright © 2011-2022 走看看