首页 > 编程知识 正文

java的三大体系,以下属于运行时异常的是

时间:2023-05-03 21:18:43 阅读:34730 作者:1552

Throwable是Java程序中所有异常对象的基类,Throwable直接继承自Object类。

Throwable分为error(错误)和Exception(异常)。 error表示JDK有问题,与写的代码无关; Exception意味着程序本身有问题,可以通过修改代码进行修复。

其中,Exception包含两个大子类: 3358www.Sina.com/(运行时异常)和RuntimeException(检查时异常)

我在网上看到了图像。 在此共享有助于理解Throwable :

通常,Checked Exceptions将阻止执行以下异常语句:

公共类测试{ publicstaticvoidmain (字符串[ ] args ) {int age=20; system.out.println(age; //可以运行//system.out.println(1/0)//异常system.out.println(age; //不可执行}输出结果:在20 exceptioninthread ' main ' Java.lang.arithmetic exception 3360/byzeroatcom.JD.test1.test1.main中

有两种方法:

一种由异常区分,如果异常类型直接或间接继承RuntimeException,则异常为运行时异常,否则为检查时异常;另一种为http://www.Sina 不需要明确处理的为运行时异常,需要明确处理的为检查时异常。 举个例子,因为不需要显式处理public class test { publicstaticvoidmain (string [ ] aring ),所以在运行时异常//ArithmeticException中//需要显式处理,因此检查时异常}因为没有直接或间接继承}catch(classnotfoundexceptione ) RuntimeException,所以检查时异常//todo auto-generatedcate 例如,在上面的try-catch代码中,3358www.Sina.com/检查语句块是否存在异常,如果存在异常,则进入http://,并且可以添加多个catch语句块请注意,每个语句块可以各写一个异常类型,运行时遵循最近的原则(距try语句块的距离),但按异常类型范围父类的顺序向下添加。是否需要进行显式处理语句块可以添加到catch语句块之后,无论try中的语句是否异常。

举个例子来详细看看吧:

public class test3{ publicstaticvoidmain (字符串[ ] args ) {try {String str=null; 系统. out.println (str.length () ); //由于此处异常,所以不执行下一行语句,而是直接执行catch语句块中的class.forname('fafsdf ); }catch(arithmeticexceptione )//算术异常System.out.println(e ) e; }catch(nullpointerexceptione )//null指针异常是因为上述异常是null指针异常,所以该catch语句块中的System.out.println(e ) e } catch (运行时exceptione )//运行时异常System.out.println(e ) e; }catch(exceptione )//异常System.out.println(e ) e; ) finally(system.out.println ) )无所谓,我来执行) ); }}输出结果: java.lang.NullPointerException我该怎么办

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。