213、214 异常

213 异常

file:///C:/Users/13833123813/Desktop/jdk-17_doc-all/docs/api/java.base/java/lang/ArrayIndexOutOfBoundsException.html

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

抛出以指示已使用非法索引访问数组。索引为负数或大于或等于数组的大小。

--------------------------------------------------------------

(module)myException

(package)it01e213

class)ExceptionDemo

--------------------------------------------------------------

扫描二维码关注公众号,回复: 13309013 查看本文章

package it01e213;

public class ExceptionDemo {

    public static void main(String[] args) {

        //提前写好的代码:method方法、int数组、写屏数组的[1]元素

        method();

    }

    public static void method(){

        int[] arr = {1,2,3};

        System.out.println("arr[1]:"+arr[1]);

        System.out.println("明知没[3]却要写屏[3]:"+arr[3]);

        //OUTPUT:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

        // Index 3 out of bounds for length 3

        //那当然会变成索引越界ArrayIndexOutOfBoundsException

    }

}

> 【throwable类】

public class Throwable

extends Object

implements Serializable

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 throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. 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类是Java语言中所有错误和异常的超类。Java虚拟机只能抛出作为此类(或其子类之一)实例的对象,或者可以通过Java throw语句抛出这些对象。类似地,只有这个类或它的一个子类可以是catch子句中的参数类型。为了对异常进行编译时检查,Throwable和Throwable的任何子类如果不是RuntimeException或Error的子类,则被视为检查异常。

-

Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).

两个子类Error和Exception的实例通常用于表示发生了异常情况。通常,这些实例是在异常情况的上下文中新创建的,以便包含相关信息(例如堆栈跟踪数据)。

A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Over time, a throwable can suppress other throwables from being propagated. Finally, the throwable can also contain a cause: another throwable that caused this throwable to be constructed. The recording of this causal information is referred to as the chained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.

throwable包含其线程创建时的执行堆栈快照。它还可以包含提供有关错误的更多信息的消息字符串。随着时间的推移,一个可丢弃文件会抑制其他可丢弃文件的传播。最后,throwable还可以包含一个原因:另一个throwable导致构建这个throwable。这种因果信息的记录被称为链式异常设施,因为原因本身可能有一个原因,以此类推,导致一系列由另一个原因引起的异常。

-

One reason that a throwable may have a cause is that the class that throws it is built atop a lower layered abstraction, and an operation on the upper layer fails due to a failure in the lower layer. It would be bad design to let the throwable thrown by the lower layer propagate outward, as it is generally unrelated to the abstraction provided by the upper layer. Further, doing so would tie the API of the upper layer to the details of its implementation, assuming the lower layer's exception was a checked exception. Throwing a "wrapped exception" (i.e., an exception containing a cause) allows the upper layer to communicate the details of the failure to its caller without incurring either of these shortcomings. It preserves the flexibility to change the implementation of the upper layer without changing its API (in particular, the set of exceptions thrown by its methods).

可丢弃的对象可能有原因的一个原因是,抛出它的类构建在较低层的抽象之上,而上层的操作由于较低层的失败而失败。让下层抛出的可丢弃对象向外传播是不好的设计,因为它通常与上层提供的抽象无关。此外,这样做会将上层的API与其实现的细节联系起来,假设下层的异常是已检查的异常。抛出“包装异常”(即,包含原因的异常)允许上层将故障的详细信息传达给其调用方,而不会产生这些缺点。它保留了在不更改上层API(特别是其方法引发的异常集)的情况下更改上层实现的灵活性。

-

A second reason that a throwable may have a cause is that the method that throws it must conform to a general-purpose interface that does not permit the method to throw the cause directly. For example, suppose a persistent collection conforms to the Collection interface, and that its persistence is implemented atop java.io. Suppose the internals of the add method can throw an IOException. The implementation can communicate the details of the IOException to its caller while conforming to the Collection interface by wrapping the IOException in an appropriate unchecked exception. (The specification for the persistent collection should indicate that it is capable of throwing such exceptions.)

第二个原因是抛出它的方法必须符合通用接口,该接口不允许该方法直接抛出原因。例如,假设一个持久性集合符合集合接口,并且它的持久性是在java.io上实现的。假设add方法的内部可以抛出IOException。通过将IOException包装在适当的未检查异常中,实现可以将IOException的详细信息传递给其调用方,同时遵守集合接口。(持久性集合的规范应表明它能够引发此类异常。)

-

A cause can be associated with a throwable in two ways: via a constructor that takes the cause as an argument, or via the initCause(Throwable) method. New throwable classes that wish to allow causes to be associated with them should provide constructors that take a cause and delegate (perhaps indirectly) to one of the Throwable constructors that takes a cause. Because the initCause method is public, it allows a cause to be associated with any throwable, even a "legacy throwable" whose implementation predates the addition of the exception chaining mechanism to Throwable.

原因可以通过两种方式与throwable关联:通过将原因作为参数的构造函数,或通过initCause(throwable)方法。希望允许原因与之关联的新可丢弃类应提供接受原因的构造函数,并委托(可能间接地)给接受原因的其中一个可丢弃构造函数。因为initCause方法是公共的,所以它允许一个原因与任何可丢弃的对象相关联,甚至是一个“遗留可丢弃对象”,其实现早于向可丢弃对象添加异常链接机制。

-

By convention, class Throwable and its subclasses have two constructors, one that takes no arguments and one that takes a String argument that can be used to produce a detail message. Further, those subclasses that might likely have a cause associated with them should have two more constructors, one that takes a Throwable (the cause), and one that takes a String (the detail message) and a Throwable (the cause).

按照惯例,Throwable类及其子类有两个构造函数,一个不接受参数,另一个接受可用于生成详细信息的字符串参数。此外,那些可能有原因关联的子类应该还有两个构造函数,一个接受Throwable(原因),另一个接受String(细节消息)和Throwable(原因)。

-

>【error】extends throwable

class Error

java.lang.Object

java.lang.Throwable

java.lang.Error

All Implemented Interfaces:

Serializable

Direct Known Subclasses:

AnnotationFormatError, AssertionError, AWTError, CoderMalfunctionError, FactoryConfigurationError, FactoryConfigurationError, IOError, LinkageError, SchemaFactoryConfigurationError, ServiceConfigurationError, ThreadDeath, TransformerFactoryConfigurationError, VirtualMachineError

类Error

java.lang.Object

java.lang.Throwable

java.lang.Error

所有实现的接口:

可序列化

直接已知子类:

public class Error

extends Throwable

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

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

公共类Error

可抛弃的

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

方法不需要在其throws子句中声明在方法执行期间可能抛出但未捕获的任何错误子类,因为这些错误是不应该发生的异常情况。也就是说,为了编译时检查异常,错误及其子类被视为未检查的异常。

>【exception】extends throwable

public class Exception

extends Throwable

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

The class Exception 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 throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

公共类Exception

可抛弃的

类异常及其子类是Throwable的一种形式,表示合理的应用程序可能希望捕获的条件。

类异常和任何不属于RuntimeException子类的子类都是检查异常。如果检查的异常可以通过方法或构造函数的执行抛出并传播到方法或构造函数边界之外,则需要在方法或构造函数的throws子句中声明。

检查异常是编译时就要修改的异常

runtime是运行时异常

214 JVM的默认处理方案

JVM的默认处理方案

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

把异常的名称、原因、位置等信息输出在控制台

程序停止执行

猜你喜欢

转载自blog.csdn.net/m0_63673788/article/details/121508286
213
今日推荐