By what circumstances will someone "try ... to catch"? Does this apply to libraries?

Suppose you are developing an application (WebApp, Desktop, something else), and you need to define exception handling for the code. Since you want to make things reusable, what logic should you follow when handling exceptions? Are there any patterns?

For example, if some kind of DLL is going to use the network, and the network is unreliable, then it may be wise to redefine the exception handler for this function in order to repeat the request.

How does a developer approach handle exceptions for all common development aspects? Is there any template or template?

When you do things like Network, File, IO, Active Directory, and the Web, I'm sure there are preliminary best practices for solving the most common and problematic issues today. Where are they located?

+3
source share
5 answers

An exception throw should, of course, only be performed in an exceptional situation.

For processing, I would say that there are two circumstances when you want to catch an exception:

when translation is done

  • To error code
  • To another exception
  • To something that the user can understand.

on the border of the module

  • Return from COM calls, for example (to infrastructure without exception)
  • Return from the stream (no one will catch it otherwise)
  • ( , .)

, try-catch-clean-rethrow, RAII ... . , . , , .


MakerOfThings7 , .

"- ", , , .

, , , . . . ? , , MemoryStreamException, FileException DatabaseException. , 15 , .

, , . , DataStorageException . .

, , , , .

+4

- , . API, , .

, , .

:).

0

:

  • .

  • , . , .

, .NET, , .

0

You answered your question. In any case, if there is a possibility of an error that goes beyond the scope / scope of your application, it is advisable to place error handling. You will never have too much error handling. Just make sure you are not coding by exception.

0
source

Exceptions are useful when debugging. They should be avoided if possible because you need to know your control flow.

0
source

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


All Articles