213、214 异常

6 篇文章 0 订阅

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

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

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会做默认的处理

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

程序停止执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java异常是在程序运行过程中发生的错误或异常情况。它们可以分为两种类型:可检查异常(checked exceptions)和不可检查异常(unchecked exceptions)。 可检查异常是在编译时强制要求处理的异常,即必须使用try-catch块或throws语句来处理这些异常。常见的可检查异常包括IOException、SQLException等。 不可检查异常是指RuntimeException及其子类的异常,它们是由程序错误引起的,通常是由编程错误导致的。这些异常在编译时不需要强制要求处理,但也可以选择进行处理。常见的不可检查异常包括NullPointerException、ArrayIndexOutOfBoundsException等。 在Java中,异常处理通过try-catch-finally机制来实现。在try块中编写可能抛出异常的代码,然后使用catch块捕获并处理异常。如果没有适合的catch块来处理异常,则可以使用finally块来执行一些清理操作,无论是否发生了异常。 以下是一个简单的示例代码: ```java try { // 可能抛出异常的代码 // ... } catch (ExceptionType1 e1) { // 处理ExceptionType1类型的异常 // ... } catch (ExceptionType2 e2) { // 处理ExceptionType2类型的异常 // ... } finally { // 清理操作 // ... } ``` 除了try-catch-finally机制外,还可以使用throws关键字声明方法可能抛出的异常,将异常的处理责任交给调用者。 ```java public void doSomething() throws SomeException { // 可能抛出SomeException异常的代码 // ... } ``` 总结一下,Java异常是在程序运行过程中发生的错误或异常情况。通过try-catch-finally机制可以捕获和处理异常,而throws关键字可以声明方法可能抛出的异常。处理异常可以提高程序的健壮性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值