Java基础之异常详解

本文详细探讨了Java中的异常体系,包括Exception和Error的区别,Error通常表示不可恢复的严重问题,而Exception是可预见的异常,分为受检异常和运行时异常。受检异常需要在编译时处理,运行时异常则在运行时出现,如NullPointerException和ArrayIndexOutOfBoundsException。面试中,理解这些概念对于Java开发者至关重要。
摘要由CSDN通过智能技术生成

面试提问

  1. Exception和Error的区别
  2. 运行时异常和受检异常的区别
  3. 写出几种常见的运行时异常

Exception和Error的区别

Error和Exception都继承自Throwable。

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java {@code throw} statement. Similarly, only this class or one of its subclasses can be the argument type in a {@code catch} clause.

Throwable类是Java语言中所有错误和异常的超类。 Java虚拟机仅抛出属于此类(或其子类之一)的实例的对象,或者Java {@code throw}语句可以抛出该对象。 同样,在{@code catch}子句中,只有此类或其子类之一才能成为参数类型。

 Throwable是所有异常、Error的基类。 只有由Throwable衍生出来的子类才能被程序抛出,并且能被程序catch到,如果一个类和Throwable不是继承的关系的话,那么是不能去throw它,也不能去catch它的。

Error解读:

 An {@code Error} is a subclass of {@code Throwable} that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The {@code ThreadDeath} error, though a "normal" condition, is also a subclass of {@code Error} because most applications should not try to catch it.

{@code Error}是{@code Throwable}的子类,表示合理的应用程序不应尝试捕获的严重问题。 大多数此类错误是异常情况。 {@code ThreadDeath}错误虽然是“正常”情况,但它也是{@code Error}的子类,因为大多数应用程序不应尝试捕获它。

 Error是不应该被catch掉的,Error通常情况下是指在我们程序正常运行的时候它不大可能出现情况,当Error出现的时候,一般会导致我们的程序崩溃,并且处于一个不可恢复的状态,比如说:OutOfMemoryError,这个时候程序已经崩溃了,是不可能通过捕获异常来进行恢复的,所以说我们在程序中是不应该捕获这些严重的错误的。

Exception解读:

The class {@code Exception} and its subclasses are a form of {@code Throwable} that indicates conditions that a reasonable application might want to catch.

{@code Exception}类及其子类是{@code Throwable}的一种形式,它表示合理的应用程序可能希望捕获的条件。

“might want to catch”异常是我们推荐去捕获并且处理它的,Exception就是我们程序在运行的时候可以预见的一些意外的情况,比如说参数为空、参数类型不合法。此时我们可以对它进行判断,捕捉并处理。

 关于Exception和Error的区别的标准回答:

     Exception和Error都继承自Throwable,在Java中只有Throwable类型的实例才可以被抛出或捕获。

     Error指正常情况下不太可能出现的情况,绝大部分的Error会导致程序崩溃,处于非正常的不可恢复的状态,如OutOfMemoryError、      StackOverFlowError,是程序中不应该试图捕获的严重问题。

     Exception是程序正常运行中可以预料的意外问题,可以捕获并处理。

The Exception class  and any subclasses that are not also subclasses of RuntimeException are  checked exceptions.  Checked exceptions need to be declared in a method or constructor's {@code throws} clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

 受检异常就是编译时被强制检查的异常,我们在方法申明的时候要去申明这些异常,常见的受检异常比如说:SQLException,IOException、FileNotFoundException。 我们调用这些抛出受检异常的方法的时候,调用者需要对抛出的异常进行捕获处理或继续往上抛出的。

其他的异常称为不受检查的异常,不受检查的异常又称为运行时异常,这些异常只有在程序运行的时候才会出现,所有的运行时异常都是继承自RuntimeException,比如说空指针异常,NullPointerException,还有类型转换异常ClassCastException,还有我们经常遇到的数组越界异常,IndexOutOfBoundsException、ClassNotFoundException异常,不受检查的异常通常是在编码中可以避免的逻辑错误。

代码解释:

public static void test() throws NullPointerException {
   System.out.println("you are best one!");
}
public static void main(String[] args) {
   //对于test方法抛出的运行时异常,不需要强制要求处理,
   // 而如果抛出的是受检异常(如SQLException)则强制要求处理!
   SingletonCAS.test();
}

 关于运行时异常和受检异常的区别的标准回答:

      受检异常:在编译时被强制检查的异常,在方法的声明中throws声明的异常。(举例:ClassNotFoundException、IOException)

      不受检异常:也称为运行时异常,通常是在编码中可以避免的逻辑错误,根据需求来判断如何处理,不需要在编译器期间强制处理。

关于写出几种常见的运行时异常的标准回答:

      运行时异常RuntimeException是所有不受检查异常的基类,NullPointerException、ClassCastException、NumberFormatException、ArrayIndexOutOfBoundsException等。

Throwable的大致分类:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值