Java – what should you do?

Java checked exceptions sometimes force you to catch checked exceptions that you think will never be thrown Best practice requires that you wrap it in an unchecked exception and re throw it just in case In this case, what exception type of packaging do you use?

What exceptions would you include in the case of "/ / should never happen"?

Solution

I haven't, but I will do it as follows:

>Let it extend error instead of exception / runtimeException, since it is unlikely to be caught by an error handler that is not prepared for it; > Wrap it in some static utility calls:

Example:

public static void doAssert(boolean result,String reason) {

    if ( !result ) 
        throw new OMGWereDoomedError("Assertion Failed: " + reason + " .");
}

I prefer these wrappers because I can change the implementation as needed

In addition, I don't advocate assertions because I don't always control runtime flags, but I do want execution to stop suddenly when the system is compromised

Think about it:

>Show users sorry, we can't process your payment page; > Confirm the results to the user even if the system is in an uncertain state

Which would you choose?

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
分享
二维码
< <上一篇
下一篇>>