Should exceptions and exceptions be excluded?

I have read a lot of exception messages lately, and I am wondering if exceptions should be detected. I read that if you want your application to recover from an error, you use checked exceptions. However, if you cannot handle the checked exception, you either transfer it to another checked exception to transfer it to another layer; for example, you are SqlException , or you are throwing an exception. However, if you catch unchecked exceptions? Are there exceptions that exclude programming errors that you don't check? Should they just pop out of your application?

+4
source share
6 answers

Should unchecked exceptions be detected and processed?

The answer is:

  • It depends on what is the exception.

  • It depends on why the exception was thrown. Was it "expected"? Was it due to a mistake, poor input or an environmental problem? Or something different?

  • It depends on whether there is a good way to recover. This usually depends in part on previous criteria.

If the exception is unexpected, the cause of the exception is undefined, and / or if there is no reliable recovery strategy, if you still catch the exception, then it is usually best to resolve the exception and make sure it is received. reported / entered at the top level.

And if the exception is Error , the general rule is that you should NOT attempt to repair. And that includes a StackOverflowError and (in particular) OutOfMemoryError . Error exceptions indicate a problem that is difficult (or impossible) to fix safely, and the best strategy is to allow or force the application to terminate.


What do you mean when reporting / logging in at the top level? Do you mean, for example, to catch it at the user interface level and show a dialog, enter it, etc.?

I mean, the exception and its stack trace must be written to the application log file so that maintainers can see evidence of the problem. Whether you are also trying to explain the problem to the end user (and how you do it) is a separate issue.

The top level can be the main method, a child thread, or the run method ... or an unhandled exception handler. Essentially, this is where the exception will ultimately β€œpop up” if it is not detected. Details will depend on the architecture of your application.

+9
source

You must catch the exception - checked or unchecked - if you can handle it in a meaningful way to recover from the problem. Normally, you should not catch an exception unless you have a good way to handle this.

I saw too much code that "handles" exceptions by executing e.printStackTrace() and then continuing as if nothing had happened. Ignoring such a problem usually leads to other problems later.

+5
source

Nothing prevents you from catching exceptions at runtime.

The trend, at present, is to use more and more exceptions at run time and fewer and fewer exceptions checked. Spring, Hibernate, and the latest Java EE specifications use exceptions exclusively at run time. This makes reading business code easier and less cumbersome.

Runtime exceptions are usually designed to prevent them from being caught or caught at the bottom of the call stack at the user interface level to display an error message, as this is usually the only thing you can do when such an exception occurs.

+4
source

The main difference between Checked and Unchecked Exception is that you need to explicitly handle the first or distribute it in the inheritance hierarchy , while this is not required for a later one.

In addition, a CheckedException extends from java.lang.Exception , and UncheckedExceptions extends from java.lang.RuntimeException , which are not needed for processing. Note that RuntimeException itself is a subclass of Exception .

Given that all the information is above, if you handle a unchecked exception , then its fine. It will work fine and control will go to the corresponding catch . But you should not do that. If you really do not need it, and you have the right way to deal with them.

  • For example, for example:. You must handle an IllegalArgumentException , which is a unchecked exception .

  • And then you should not handle errors like: - StackOverflowError . Like, you don’t know why this problem could happen, and what might be the appropriate way to handle it. So just leave this in the JVM. Errors are unchecked exception from which it is impossible to recover.

For more information, see the following links: -

+1
source

The choice for checked exceptions is usually: you just add it to throws , if you can't do anything about it, catch and convert it to a new exception with the original as a reason if you can't do anything useful about it, but can add information and catch and do something about it, if possible, and also in the right place in the code to do something.

Uncontrolled exceptions are a bit trickier. In an ideal world, they should point out programming / logic errors and be handled pretty much like a statement failure, not caught and especially not swallowed.

Some unchecked exception coming from the standard Java library or other libraries should, in my opinion, really be checked by exceptions, but this is not so. These are cases where the caller must recognize that such exceptions can go through them, even if they do not catch them. For unchecked exceptions that can also be checked by exceptions, basically the same rules: catch them if you want to do something with them, otherwise let them bubble. If you are creating a library (even if it is only internal to the application), and an unchecked exception is really something to catch later, then you might want to catch it and throw it into the checked exception in the library code.

In general, try to avoid removing excluded exceptions. Check the input, so you do not need to catch , and if the exception is still thrown, then this is an error and should be left unmapped. Only catch if this is the only or at least the best way.

To be fair for Java libraries, the language was developed in an era when the IDE did not immediately complain about the lack of offers about throws and offered to automatically fill them, so their uncontrolled can be justified, reducing the load on the developer. But with modern tools there really is no excuse.

And then, as mentioned in other answers, there are excluded exceptions that you should not catch.

+1
source

should excluded exceptions be excluded?

Yes and no. Depends on which exception is thrown.

Excluded exceptions excluding programming errors that you do not check?

You can write a catch to catch the thrown exception, but again it depends on whether you should . If you do this, it is possible that the error remains unresolved for a long time, and by the time it is detected, the size will also be resized.

Should they just bubble out of your application?

If they occur, try to solve their cause (if it is "calm"). Do not catch them always, usually Hard and Fast.

-1
source

Source: https://habr.com/ru/post/1444352/


All Articles