I heard that it is possible, after throwing an Exception process it in catch , and to return to the point of origin of Exception itself to continue.

It should look something like this:

 try { // Вызываем исключение // Возвращаемся сюда для продолжения исполнения кода } catch (SomeEx) { // Обработка исключения и возврат обратно в вышестоящий блок try{} } 

The question is : is there really such a way?

It is necessary approximately in such a situation: there is a deeply buried code that opens a connection to the database. Below is the code for loading data from the database. So, I want to make a check on the correctness of the database (base version) at the connection opening level: if the version is incorrect, you need to give the user a message that the version is old, but you can continue executing the code to load data from it (this message is not critical).

I also thought about the post-inspection, but the fact is that the connection is opened in various places, and the post-inspection version will also have to be spread all over the code, which is not good.

  • "connection opens in various places" - not very good. Make the component that will be responsible for the connection and all that. Connection will only go through this component. And the code is more modular at the same time. - VladD
  • after exclusion, there is no "return to the point of origin". The exception is either processed on-site and the code continues, as if the exception was not, or not processed and the exception is passed to the calling function. - uilenspiegel
  • @vladd and what to do? My connection opens: * When the user opens the file selection dialog. * When a user drags a file onto a form. * When the file arrives to the application during the start as a parameter. * + The system of plug-ins is implemented, where a connection can also be opened if a connection was not transferred to the plug-in or a null connection was transferred as a parameter. Yes, I will try to make another interlayer. Now I have done so far through the template, implemented my method for opening the connection, it remains to find all the places where the connection opens, and replace it with my method - pincher1519
  • @ pincher1519: this is the way to do it. The logic of throwing-throwing exceptions pack in the layer. - VladD
  • Anyway, it doesn't work out well, I need to display the messagebox to the user about the incorrectness of the base version. Accordingly, it must be output at the stage of the code operation of the main application window ... Ie will have to drag the message from the very depths to the main window of the program ... - pincher1519

2 answers 2

It is not possible to return to Tray after an exception occurs Test connection is usually made separately from the useful bd-shny traffic. Plus, no one has canceled the transaction. Complete the transaction at the very end of the trai block, or in financially.

  • transactions on the client - evil! :) - uilenspiegel
  • Evil, I do not argue, but there are times when they very much saved me. Plus, the transaction can be carried out and not at the level of the database ... - Eugene Karpov
  • I do not make changes to the database, and I'm not interested in managing transactions. When connecting to the database, the auto-transfer mode is enabled. - pincher1519
  • In general, it is strange that you can not go back to the Tray block. debager can do it. For example windbg. someone told me this phrase: you can go back to the try block on the line of code execution error, with the exception that when a new exception occurs, this try block will no longer catch it, because he has already worked. - pincher1519
  • Well, debugger is debugger to control the code completely. With it, you can not only return to the place where the exception was created, but also execute a forced portion of the code. - Eugene Karpov

And why not just do it like that?

 ``` try { // делаем исключение } catch (SomeEx) { // обработка сообщения. возврат обратно } // продолжение. ``` 

I also thought about the post-inspection, but the fact is that the connection is opened in various places, and the post-inspection version will also have to be spread all over the code, which is not good.

This is also not entirely clear: why not just encapsulate this behavior in a separate class / method?

  • This cannot be done, because the opening of the connection is buried deep. on kolsteku 3 methods in depth. - pincher1519