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.