Java – if a runtimeException is thrown, can it be caught as an exception?

If I have a try block that throws a runtimexception subclass, can subsequent catch blocks catch it as an exception? especially:

public class MyAppException extends RuntimeException {
    // ....
}

// In some other part of the code:
try {
    // Executing this results with doSomething() throwing a MyAppException.
    int x = doSomething();
} catch(Exception exc) {
    // Does the thrown MyAppException get caught here?
}

My idea is yes, because runtimeException extends exception But I have some production code that doesn't fit this way Obviously, if the answer is no, it is my answer; Otherwise, I need to understand why my code is broken Thank you in advance!

Solution

Yes It will catch the runtimeException, but if any exception occurs in the catch block, you must catch it again

I suggest you deploy locally and debug the code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>