Multiple try-catch blocks VS one large try-catch block [in stream]

I found one recommendation that says that we should try to keep one try / except statement for the stream.

I have a class that implements Runnable (Java, but it really doesn't matter). It has several blocks, each of which is surrounded by its own try-catch block with logging and processing. Each block generates the same exception class.

Do you think I should expand the collection of exceptions that creates an exception for each operation, put all the blocks under one try-catch block and handle each differently? Here is an example of what I have here .

Thanks in advance.

+6
source share
1 answer

The above example suggests a single catch block for clarity and maintainability. I agree with that, this is good advice. Are you worried about performance? Theoretically, if you do things between exception throwing blocks, multiple try / catch blocks will be faster. But the difference would be very small, and maintainability would suffer. Go with the only attempt to catch.

+3
source

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


All Articles