How to get exceptions that should not be caught in Java?

I would like to create a custom exception that will be detected at runtime. Currently, I have to either add throws to the function or surround it with try catch. I want this exception not to be caught at all. This is a fatal exception and will show the programmer a mistake and what he can do to fix it. This is for checking an abstract class if initialization was first performed. I would like it to act as a NullPointerException, because when it occurs, the program crashes.

thanks

+4
source share
5 answers

Subclass of RuntimeException instead of Exception.

I obviously donโ€™t know the design decisions behind this, but it seems that there may be a better way to achieve what you are trying to do.

+13
source

Make your own exception a subclass of RuntimeException . They can be caught in try / catch, but this is not done by the compiler.

+3
source

Your program should never just crash.

Ideally, it should ideally record the backtrace and any related information that would help debug the problem, and then exit the notification that something went wrong and where the data is being recorded.

+2
source

You need unhandled exceptions or exceptions that extend the RuntimeException class. By default, all unchecked exceptions end up in the default uncaught exception handler , which prints an exception stack trace. If you have not changed the default exception thrower to be different, the behavior you observe when throwing unchecked exceptions will be the same as the one you encounter when a NullPointerException is thrown (another exception thrown).

Note that you will not see the exception stack trace if an unchecked exception hits the caller that acts on it without printing the stack trace. There is no difference in how uncontrolled and checked (those that RuntimeException Exception instead of RuntimeException ) throw exceptions; there is only a difference in which the compiler considers checked and unchecked exceptions.

0
source

You should create a basic exception (possibly named Boobake4DomainException ) that extends RuntimeException , and then extend it for all exceptions at runtime.

Then you can just } catch (Boobake4DomainException e) { be sure upstream if it's one of your own or not. This can make your code handling problem a lot easier.

0
source

All Articles