异常

异常处理机制

  1. 抛出异常
  2. 捕获异常

异常处理五个关键字

try,catch,finally,throw,throws

快捷方式:ctrl+alt+t

package com.zhang.oop.exception;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class Demo01 {
    public static void main(String[] args) {   //捕获多个异常要从小到大
        try {        //try监控区域
            new Demo01().a();
        } catch (Error e) {       //catch(想要捕获的异常类型)
            System.out.println("程序出现异常");
        }catch(Exception e){
            System.out.println("exception");
        }catch(Throwable t){
            System.out.println("throwable");
        }finally {        //处理善后工作  可以不要finally
            System.out.println("finally");
        }
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}
posted on 2023-03-15 10:13  似初吖  阅读(23)  评论(0编辑  收藏  举报