Throwing and throwing exceptions

Hey Stack Overflow Community,

Throwing exceptions. As a rule, when do I drop and rule out, and when will I catch him?

Let's say I am faced with a situation where I have to leave due to a problem that has arisen, and I cannot recover from it. Am I throwing or catching?

I'm doing it right now:

try { // some code } catch (IOException e) { logger.info("Failed to do something, and cannot continue" + e.getMessage(), e); e.printStackTrace(); throw e; } 

Is this right to do? Would it be more appropriate if I just threw an exception? Sorry, I'm new to exceptions :)

+6
source share
6 answers

Usually you find an exception in a method when you want your program to continue to work. You throw an exception if you want a higher level method to call this method to handle the exception. For example, you can completely drop it to your Main method, which has a try..catch block (probably with different catch blocks for different exceptions), encapsulating all your method calls, and exceptions can be handled there (for example, program termination).

Remember that throwing an exception will immediately terminate the method. This affects the flow of your code. If you can have an exception in the middle of the method, and the code below cannot work, if this exception occurs, you need to either wrap the entire section in a try / catch block or throw an exception.

The general word of advice is printStackTrace () is bad. You can create the best error output yourself (and you can enable stack tracing as well as your output). Better yet, use logging.

I recommend reading this introduction to exceptions and this article, which describes the good and bad exception patterns .

+8
source

If a fatal exception occurs, catch the exception and terminate your program. Reuse without fishing will simply kill your program.

+1
source

If you know that an exception is not something that you can handle, let the exception go without catching it in the first place. There is one exception handler that catches everything and writes it to a file along with stacktrace. For example, if your program is running from the command line, you can catch everything in the main method and register it there.

+1
source


You will catch the exception when you have something to do with it. In your case, write to the log, display a message to the user and exit in an ordered manner.
You throw an exception when you can’t do anything else, when it happens.

I recommend that you use the application block to handle Microsoft corporate library exceptions . This will help you deal with your exceptions so that you can control the flow and make configuration changes.

+1
source

After catching, I would log the incident, and then do what you need to finish the job accurately and then call System.exit, but I would not select this exception again.

0
source

Bad practice is that your program terminates due to an unhandled exception. If you catch an exception that is fatal and unrecoverable, follow these steps:

  • register it
  • perform any cleaning without the need
  • shut down the program
0
source

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


All Articles