Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the throwable class

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what is the use of the throwable class, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

The publicclassThrowableextendsObjectimplementsjava.io.SerializableThrowable class is the superclass of all errors or exceptions in the Java language. It can be thrown through a Java virtual machine or Javathrow statement only if the object is an instance of this class (or one of its subclasses). Similarly, only this class or one of its subclasses can be a parameter type in the catch clause.

Throwable subclass

Instances of two subclasses, Error and Exception, are usually used to indicate that an exception has occurred. Typically, these instances are newly created in the context of an exception and therefore contain relevant information (such as stack trace data).

Throwable contains a snapshot of the thread execution stack when its thread is created. It also contains a message string that gives more information about the error. Finally, it can also contain a cause (reason): another throwable that causes this throwable to throw. This cause facility first appeared in version 1.4. It is also known as the exception chain facility because the cause itself has cause, and so on, forming an exception chain, with each exception caused by another exception.

Throwable property

One reason why throwable may have cause is that the class that throws it is built on the basis of a low-level abstraction, so that the high-level operation fails due to the failure of the low-level operation. Therefore, it is not a good design method to let the throwable thrown by the lower level propagate outward, because it is usually not related to the abstraction provided by the high level. In addition, doing so correlates the high-level API with its implementation details and considers the low-level exception to be a checked exception. Throwing a "wrapped exception" (that is, an exception containing a cause) allows a high level to exchange failure details with its caller without incurring any of the above shortcomings. This approach retains the flexibility to change the high-level implementation without changing its API (in particular, exception collections are thrown through its methods).

Another reason why a throwable may have a cause is that the method that throws it must conform to a generic interface that does not allow the method to throw the cause directly. For example, suppose that a persistent collection conforms to the Collection interface, and its persistence is implemented on the basis of java.io. Assume that IOException can be thrown inside the put method. The implementation can communicate the details of the IOException with its callers, while wrapping the IOException with an appropriate unchecked exception to make it conform to the Collection interface. The specification of a persistent collection should indicate that it can throw such an exception. )

Cause can be associated with throwable in two ways: through a constructor that treats cause as a parameter, or through the initCause (Throwable) method. For new throwable classes that want to associate cause with them, you should provide a constructor with cause and delegate (perhaps indirectly) to a Throwable constructor with cause.

The above is all the content of this article "what is the use of throwable classes?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report