Java中的异常

一.什么是异常?

 java在运行阶段形成的问题,或者错误就是异常。

二.异常的继承关系

从图中可以看出所有的异常都是由Throwable继承而来,但是在下一层,就分为:Error和Exception。Error主要是jvm方向上的错误,比如说内存不足,和请求栈深度超过最大深度。(一般遇到这种情况就是等死吧),在设计Java程序时只需要考虑Exception下的异常。

三.编写自己的异常类

编写一个异常类必须要从相应的异常类继承,最好是选择意思相近的异常类。 

package com.cjm.exception;

/**
 * 我的第一个异常类
 * 
 * @author 小明
 *
 */
public class MyFirstExceptionText {
    public static void main(String[] args) throws MyFirstException {
        for (int i = 0; i < 3; i++) {
            if (i == 2) {
                throw new MyFirstException("你抛出了一个你自己写的异常");
            }
        }
    }
}

class MyFirstException extends Exception {
    public MyFirstException() {

    }

    public MyFirstException(String str) {
        super(str);
    }
}

 

四.扑捉异常

 形式:

          try{

               //可能出现异常的代码

               }catch(类型1的异常){

       // 处理方式

                    }

     catch(类型2的异常){

       // 处理方式

                   }

 

package com.cjm.exception;

/**
 * 使用try catch来扑捉异常
 * 
 * @author 小明
 *
 */
public class TryCatchException {
    public static void f() throws MyFirstException, MysecoExpection {
        int i = 1;
        if (i == 0) {
            throw new MyFirstException("扑捉你的第一个异常");
        } else {
            throw new MysecoExpection("扑捉你的第二个异常");
        }

    }

    public static void main(String[] args) {
        try {
            f();
        } catch (MyFirstException e) {
            e.printStackTrace();
        } catch (MysecoExpection e) {
            e.printStackTrace();
        }
    }
}

class MysecoExpection extends Exception {
    public MysecoExpection() {

    }

    public MysecoExpection(String str) {
        super(str);
    }
}

五.日志 

import java.util.logging.Logger;

/**
 * 日志
 * 
 * @author 小明
 *
 */
public class MyDaily {
    public static void main(String[] args) {
        try {
            throw new MythrException();
        }catch(MythrException e) {
            System.err.println(e);
        }
        
        
    }
}
class MythrException extends Exception{
    private static Logger logger= Logger.getLogger("我的第一个日志");
    public MythrException() {
        StringWriter trace =new StringWriter();
        printStackTrace(new PrintWriter(trace));
        logger.severe(trace.toString());
    }
    
}

六.finally块

fianlly语句一定会被执行,无论在try块里是否会有异常抛出。

      try{

               //可能出现异常的代码

               }catch(类型1的异常){

       // 处理方式

                    }

     catch(类型2的异常){

       // 处理方式

                   }

       finally{

                //一定会被执行的语句      

     }

 例子:

package com.cjm.exception;

/**
 * finally块
 * 
 * @author 小明
 *
 */
public class Finally {
    public static void main(String[] args) {
        try {
            System.out.println("我的没有异常!");
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            System.out.println("这一步一定会被执行!");
        }
        
        
    }
}

package com.cjm.exception;

/**
 * finally块
 * 
 * @author 小明
 *
 */
public class Finally {
    public static void main(String[] args) {
        try {
            throw new MyFirstException("有异常抛出");
        }catch (MyFirstException e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            System.out.println("这一步一定会被执行!");
        }
    }
}

 

i

原文地址:https://www.cnblogs.com/SAM-CJM/p/9406603.html