简体   繁体   English

Java中处理所有异常的内置基类是什么

[英]What is the built in base class to handle all exceptions in Java

What is the built in base class to handle all exceptions in Java? Java 中处理所有异常的内置基类是什么? Is It Exception or Throwable?它是异常还是可以抛出?

What is the difference between two builtin classes,can someone explain me.两个内置类有什么区别,有人可以解释一下。

Below image will help you to understand Exception Hierarchy下图将帮助您了解Exception层次结构

在此处输入图片说明

Image Ref: programcreek:图片参考: programcreek:

As you can see Throwable is super class of Error and Exception while Exception deals with checked and unchecked Exception.如您所见, ThrowableErrorException 的超类,而Exception处理已检查和未检查的异常。

Exception 例外

The term exception is shorthand for the phrase "exceptional event." 术语异常是短语“异常事件”的简写。

Throwable: 可投掷:

The Throwable class is the superclass of all errors and exceptions in the Java language. Throwable 类是 Java 语言中所有错误和异常的超类。 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 throw statement.只有作为此类(或其子类之一)的实例的对象才会被 Java 虚拟机抛出或可以被 Java throw 语句抛出。 Similarly, only this class or one of its subclasses can be the argument type in a catch clause.同样,只有此类或其子类之一可以是 catch 子句中的参数类型。 For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.出于编译时异常检查的目的,Throwable 和 Throwable 的任何子类(不是 RuntimeException 或 Error 的子类)都被视为已检查异常。

The javadocs are for this. javadocs 就是为了这个。 Here you can see that Throwable is the superclass for all Exceptions and Errors .在这里您可以看到Throwable是所有ExceptionsErrors的超类。 Then you have checked and unchecked Exceptions , where the latter is RuntimeException and all its subclasses.然后您已经检查和未检查Exceptions ,其中后者是RuntimeException及其所有子类。

Remember to use Google when you're wondering about things like this, because all this information is widely available and easily found with a search engine.当您对此类事情感到疑惑时,请记住使用 Google,因为所有这些信息都可以广泛使用,并且可以通过搜索引擎轻松找到。

From the javadocs :javadocs

Class Exception类异常

java.lang.Object
 |
 ->java.lang.Throwable
    |
    ->java.lang.Exception

Hope this clears the doubt.希望这可以消除疑虑。

The above answered are very much informative.上面的回答提供了很多信息。 I just want to add:我只想补充:

  • The Master Base Class: java.lang.Object主基类: java.lang.Object

  • The Master Exception Class: java.lang.Throwable主异常类: java.lang.Throwable

  • The Master Exception Class extends Throwable : java.lang.Exception and java.lang.Error主异常类扩展了Throwablejava.lang.Exceptionjava.lang.Error

  • The Unchecked class extends Exception extends Throwable : java.lang.RuntimeException Unchecked 类扩展了Exception extends Throwable : java.lang.RuntimeException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM