Java异常处理throws,throw关键字try,catch,finally模块 红太狼 2022-07-27 11:58 197阅读 0赞 ## Throwable ## Throwable是所有的错误和异常的超类,有两大实例 * Error 一般不处理,由JVM抛出 * Exception 可预测可处理 —–编译时异常 :只要是Exception和其子类都是,除了特殊子类RuntimeException体系都必须针对性处理 —–运行时异常 :RuntimeException和其子类,直接中断功能,停止继续运行 ![这里写图片描述][20160412233400601] 这是Java异常体系: ![Java异常体系][Java] ## 异常声明 ## Java中不要求在方法头显示声明Error和RuntimeException(免检异常)。但是方法抛出的其他异常都必须在方法头给出显示声明 已告知方法调用者。 eg: public void method() throws Exception1,Exception2… \{ //do throw Exception1…… \} Java API中描述: > RuntimeException是那些可能在Java虚拟机正常运行期间抛出异常的超类 > 可能在执行方法期间抛出但未被捕获的RuntimeException的任何子类都无需在throws子句中声明 注意: 1. 自定义异常类必须继承自异常体系,否则不具备抛出异常的资格 才可以被关键字throw和throws 操作 2. 自定义异常一般继承:Exception或者RuntimeException 3. throws可以声明多个异常,throw用于抛出异常,在函数内 4. 当有一个方法有多个异常时,针对异常处理catch模块的匹配是顺序匹配,所以catch超类异常类型必须定义在子类异常类型后面,简单说就是catch越具体,越靠前,否则编译出错 ## 异常处理的基本格式 ## try \{ //需要被检测异常的代码 \} catch (异常类 变量) \{ //处理异常的代码 \} **下面这个代码块可以没有** finally \{ //一定被执行的代码 \} *异常体系的基本方法*: **String : ex.getMessage()** 返回这个对象的消息 **String : ex.toString()** 返回以下三个字符串的连接:1.异常类的全名 2.“:” 3.getMessage()方法 **void : ex.getStackTrace()** 返回栈跟踪元素构成的数组来表示这个可抛出的栈的跟踪信息 **printStackTrace()** 在控制台上打印Throwable对象以及它的调用栈的跟踪信息 实际开发中一般用第三方包 ## 关于finally模块 ## /有一种情况不会执行finally模块 catch \{ //处理 **System.exit(0);退出jvm** \} Finally \{ //即便所有catch模块都不匹配也会执行 \} **Finally用处**:通常用于关闭(释放)资源 比如数据库 连接数据库 查询 Exception 关闭连接 **注意:** * 子类覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类异常或者该异常的子类 如果父类跑出多个异常,那么子类只能抛出父类异常的子类 * 如果父类的方法没有抛出异常,那么子类覆盖式决不能抛出异常 代码演示: import java.util.Scanner; class InputException extends Exception { InputException() {} InputException(String msg) { System.out.println("……异常……"); } } class ExceptionDemo { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("输入长和宽:"); double width = input.nextDouble(); double height = input.nextDouble(); try { method(width,height); } catch (InputException ex) { System.out.println("请重新输入"); System.out.println("getMessage(): "+ex.getMessage());//返回这个对象的消息:String System.out.println("toString(): "+ex.toString());//返回以下三个字符串的连接:1.异常类的全名 2.“:” 3.getMessage()方法:String System.out.println(ex.getStackTrace());//返回栈跟踪元素构成的数组来表示这个可抛出的栈的跟踪信息:void ex.printStackTrace();//在控制台上打印Throwable对象以及它的调用栈的跟踪信息 //jvm默认的异常处理机制就是调用异常对象的这个方法。 } } static double method(double width,double height) throws InputException { if (width<=0 || height<=0) { throw new InputException("输入不合理"); } else return width*height; } } ![这里写图片描述][20160412234833122] [20160412233400601]: /images/20220724/dfd6fa783ca140fe9003c42cae0b41c4.png [Java]: /images/20220724/f4519f067f6b4dac8c4d7ca81d6cbc31.png [20160412234833122]: /images/20220724/a37f975d53994e6ca2f841cbfb4127dd.png
相关 Java异常处理机制:使用throws关键字示例 在Java中,当我们编写可能会抛出异常的方法时,我们可以使用`throws`关键字来声明这个方法可能产生的异常。 下面是一个简单的例子,我们定义一个计算阶乘的方法,但阶乘操作 ﹏ヽ暗。殇╰゛Y/ 2024年09月11日 22:00/ 0 赞/ 16 阅读
相关 java异常处理(java异常处理的五个关键字) Java的异常处理语法 虽然有点多 但是对你有用 好好去看看吧 异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if... 快来打我*/ 2023年09月25日 08:23/ 0 赞/ 82 阅读
相关 Python 处理异常栈模块——traceback 模块 Python 处理异常栈模块——traceback 模块 参考文章: [(1)Python 处理异常栈模块——traceback 模块][1_Python _traceba 约定不等于承诺〃/ 2022年09月14日 09:42/ 0 赞/ 147 阅读
相关 Java异常处理throws,throw关键字try,catch,finally模块 Throwable Throwable是所有的错误和异常的超类,有两大实例 Error 一般不处理,由JVM抛出 Exception 红太狼/ 2022年07月27日 11:58/ 0 赞/ 198 阅读
相关 Java异常处理-throws关键字 throws关键字 1.在定义一个方法的时候可以使用throws关键字声明,使用throws声明的方法表示此方法不处理异常,抛给方法的调用者处理。 2. 刺骨的言语ヽ痛彻心扉/ 2022年06月08日 11:46/ 0 赞/ 182 阅读
相关 Java转Python之函数、模块和异常处理 函数 声明方式与JS类似,声明的关键字由function改成def,用大括号改成缩进方式来限定范围。 参数的赋值可以用关键字方式,这个是Java不具备的,有点像Spr 约定不等于承诺〃/ 2022年06月03日 03:13/ 0 赞/ 157 阅读
相关 Java中处理异常中return关键字 Java中,执行try-catch-finally语句需要注意: 第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会 比眉伴天荒/ 2021年06月10日 20:42/ 0 赞/ 402 阅读
相关 Java异常架构与异常关键字 文章目录 * Java异常简介 Java异常架构 * 1. Throwable * 2. Error(错误) ... 你的名字/ 2020年06月14日 09:59/ 0 赞/ 726 阅读
还没有评论,来说两句吧...