What happened to the exceptions when the attempt finally used only instead of catching and how it is handled?

when a programmer uses a try block without catch

like this

PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(c); } finally { pm.close(); } 

what happened to the exception and how can it be handled later?

I am trying to learn it from the Internet, but there is no clear result for it ...

+5
source share
5 answers

If you do not specify a catch , you are mainly responsible for handling the exception for the calling method.

So, if your method does not catch one or more Exceptions from the try block, and the exception is raised in the method block, it will be returned to the caller.

The finally block ensures that if something bad happens in the try block, at least you will have the opportunity to close / release any related resources before the exception is returned to the caller.

+1
source

Yes. It makes sense. In your case, even an exception occurs, the programmer wants to simply ignore the exception and close the PersistenceManager . At this level, the exception is ignored, and at the top level, someone can catch it. He delegates there.

+1
source

catch is used to detect a specific exception based on your needs. Even you can use the general exception in catch to catch the exception. If you use an exception exception, the exception will be handled by default.

+1
source

At this point, the exception is not handled and will still pop up. It will be processed later.

+1
source

This is a common idiom for handling (or better: not handling) exceptions in code that allocates resources.

If an exception occurs in the try block, this block will terminate abruptly. Then the corresponding catch command will be checked. If this is not found, if the external block terminates abruptly, which in most cases means that the method will throw this exception for its caller.

But before that, the finally block will always be executed after exiting the try block (and even after processing the excluded catch). This is the best place to host any dedicated resources. This way you will make sure that you have cleared after your work whether an exception occurred or not.

+1
source

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


All Articles