Java-10.6 finally (3)-exception loss

Source: Internet
Author: User

Java-10.6 finally (3)-exception loss

In this section, we will discuss the exception loss caused by the use of finally.

This is actually a very accidental mistake, which we generally will not make.

 

package com.ray.ch10;public class Test {public static void main(String[] args) throws Exception2 {try {throw new Exception1(Exception1);} finally {throw new Exception2(Exception2);}}}class Exception1 extends Exception {public Exception1(String msg) {super(msg);}}class Exception2 extends Exception {public Exception2(String msg) {super(msg);}}

Output:

 

Exception in thread main com. ray. ch10.Exception2: Exception2
At com. ray. ch10.Test. main (Test. java: 9)

 

The output shows that prediction1 is completely missing.

 

The other is simpler loss.

 

package com.ray.ch10;public class Test {public static void main(String[] args) {try {throw new Exception();} finally {return;}}}

Output:

 

(None)

 

Summary: This section provides an example to describe how to use finally to cause abnormal loss. However, we generally use try catch to avoid exception loss.

 

 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.