异常-----Throwable(Error && Exception(RuntimeException&& 非RuntimeException))及自定义异常

异常:就是程序出现了不正常的情况

异常体系:

  • Throwable
    • Error:严重问题不需要处理
    • Exception:称为异常类,它表示程序本身可以处理的问题
      • RuntimeException:在编译期是不检查的,出现问题,需要我们来回修改代码
      • 非RuntimeException:编译器就必须要处理的,否则程序不能通过编译,就更保护能正常运行了。

JVM的默认处理方案:如果程序出现了问题,我们没有任何处理,最终JVM会默认的处理

  • 把异常的名称,异常原因及异常的位置等信息输出在控制台
  • 程序停止运行

异常处理的方案:

  • try……catch……
    try{
       可能出现异常的代码;
    }catch(异常类名 变量名){
            异常处理的代码;
    
    }
    • 格式:
    • 执行流程:程序熊try代码里边开始执行,出现异常,会自动生成一个异常对象,该异常对象提交给Java运行时系统,当Java运行时系统收到异常对象时,会到catch中找匹配的得异常类,找到后进行异常处理,执行完毕后,程序还是可以往下执行。
  • throws

Throwable 的成员方法:

  • public String getMessage​()      //返回throwable的详细信息字符串
  • public String toString​()             //返回可抛出的简短描述
  • public void printStackTrace​()       //把异常的错误信息输出到控制台

编译时异常和运行时异常的区别:

        Java中的异常被分为两大类:编译时异常和运行时异常,也被称为受检异常和非受检异常,所有的RuntimeException类及其子类被称为运行时异常,其他的异常都是编译时异常。

  • 编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译
  • 运行时异常:无需显示处理,也可以和编译时异常一样处理

 异常处理:throws

  • 格式:  throws 异常类名;    //注意:这个格式是跟在方法的括号后边的

  • 编译时异常必须要进行处理:如果采用throws这种方案,将来谁调用谁处理
  • 运行时异常可以不处理:出问题后需要我们来回修改代码

 自定义异常:

  • 格式
public class 异常类名 extends Exception{
        无参构造
        带参构造
}

 throws  &&  throw的区别:

  • 位置区别:throws用在方法名之后,跟的是异常类名;throw用在方法体内,跟的是异常对象名;
  • 处理对象:throws表示抛出异常,由该方法的调用者来处理;throw也表示抛出异常,由方法体内的语句处理。
  • 异常的存在性:throws表示出现异常的一种可能性,并不一定会发生这些异常;而执行throw一定是抛出了某种异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.awt.Component; import javax.swing.JOptionPane; /** * This class ExceptionManager and its subclasses are a form of * Exception. It is used to wrap all the Throwable instances * and handle them in a unified way. It will show the information which consists of * StackTraces and Messages by using JOptionPanel. * * @author Estelle * @version 1.0 * @see java.lang.Exception * @since jdk 1.5 */ public class ExceptionManager extends RuntimeException { private static final long serialVersionUID = -6963187366089365790L; /** * This field alerter is used to show the information the Class offered. * * @see javax.swing.JOptionPane */ private JOptionPane alerter; /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg). * * @param msg The message will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(String msg). */ public static ExceptionManager wrap(String msg){ return new ExceptionManager(msg); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(Throwable throwable). * * @param throwable The cause will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(Throwable throwable). */ public static ExceptionManager wrap(Throwable throwable){ return new ExceptionManager(throwable); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg,Throwable throwable). * * @param msg The message will pass the specified constructor * @param throwable The cause will pass the specified c

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值