Is checking exceptions in code or using try-catch best practice in Java?

Someone told me that catching all exceptions is not necessarily a good practice (e.g. NullPointerException). I am looking for an explanation when this is good, when it is not, and why it is so: D

Thanks!!

badPanda

+6
java exception exception-handling try-catch
source share
7 answers

In addition to the checked exceptions, Bozho logs usually handle exceptions that you expect, no matter how perfect your code is (i.e. the network cable is disconnected and you will catch the IO exception). You declare that methods throw checked exceptions, as other code must deal with handling these exceptions.

Non-excluded exceptions are usually used for unforeseen conditions, for example, NullPointerExceptions often fail because the program had to throw a checked exception, filter data, set default values, etc. They are uncontrollable and often unpredictable, so you do not need to catch them.

This is not true 100% of the time, but as a general approach, how it works, especially in Java.

+3
source share

In short:

Checked exceptions are for catching.

It is impossible to exclude exceptions and errors. (these are subclasses of RuntimeException and Error ).

This is because checked exceptions are "expected" and the program can repair them. Excluded exceptions are those of which the program cannot recover (easily).

The Sun tutorial says (this is about what exception you should create, but it is also informative on the other hand - i.e. when using exceptions):

The following is an example: if the client can reasonably expect recovery from the exception, make it a checked exception. If the client cannot do anything to recover from the exception, make it an uncommitted exception.

+5
source share

And adding bozho to the answer, sometimes it depends on the type of application, as in some cases you may have to process it in order to do some work for this, and in some cases you can leave it to the caller or, ultimately account, complete the job.

+2
source share

The title of your question seems to be asking if it is better to check and handle the error conditions in the code, or is it best to just put all this in a try block and handle the exceptions in catch.

If it's simple, be sure to check for an error and handle it, rather than using try-catch. For example, if you can deal with invalid input by checking it and printing out the message type "try again", you will not use the attempt.

I like to think this way: if I can carefully circumvent the error introduced as a result of the exception, check the conditions that will cause it and deal with them. If you can’t easily check the conditions or cope with them easily, use the handling trick.

+2
source share

Exceptions that are not caused by programming errors and / or can be repaired should be detected. There should not be others. In java, exceptions that are supposed to catch are usually checked, but RunTimeExceptions and Errors are not.

A null pointer exception is caused by a programming error, that is, a missing null check so that it cannot be caught. However, there may be situations when you want to catch a RunTimeException solely to catch it before throwing it back.

+1
source share

RuntimeExceptions can (most cases) be considered "programming errors." For example, consider pulling an object from the stack before checking to see if it contains any elements. In the correct implementation, some isEmpty () method must be set to check the state of the stack. If the programmer should be lazy or forget to check the stack, an exception should be indicated indicating a programming error.

These exceptions should not be caught. The idea is to minimize the program and inform the programmer of its error.

Checked exceptions, on the other hand, as others have pointed out, are exceptions from which program recovery is expected. Although this sounds good in practice, checked exceptions often occur when you cannot do anything to solve the cause of the exception. This way you get a lot of code patterns (i.e. try-catch blocks whose exception blocks do nothing but register or print the reason for the exception). Afaik, no new programming language supports checked exceptions because of this (even JavaFX).

+1
source share

I always implement try...catch(Throwable) (Throwable is really a mistake, and your application should not perform any further operations after it received one of them) at ONE point of my code, when it is very important to know what happened It can be registered. This is usually the main place.

I also have an attempt ... catch (exception) in a runnable class or class that processes, for example, one record, which can be processed independently of the others. In this case, the application must move on even if part of its processing fails - it doesn’t matter if I know which exception will be thrown or not - I will catch the exception, I will write it, I will interrupt this processing record, and I will move on .

The rule of thumb is that you should catch an exception if you are going to do something (stop processing something, start an alternative procedure or go to it if you know what you are doing) if you are not going to do anything, don't catch it.

And don't use the try...catch IDE creator to hide your exception, add exceptions to the method signature instead.

0
source share

All Articles