Use Convention (s)

What is an agreement for Try / Catch outside of clearly needed places (for example, getting certain user input)? Although code reuse is an important idea for OOP, is it expected that every (example) access to the array should have try / catch? First of all, is it a design decision of what can throw an exception, and those parts of the program that need to be well adjusted to never throw an exception?

Ex. An array of playing cards - it should always be 52 cards. No attempt / catch is required. Or, since an exception from the array outside the borders can cause a runtime error, and the deck can be used later when the jokirs are added, and then enter it now?

+4
source share
7 answers

Your example does not work a bit.

Assuming you have an array of 52 elements, the correct way to access it is to check that your index is within the array before you even try to access it. This is much better than just wrapping the code in try .. catch there.

Try Catch to help save us from yourself. If this method is encoded correctly, there is very little reason for using them. Correctly, I mean that you check that your inputs are within the expected ranges, and you have tried enough to know that the code will not be "excluded". Of course, the β€œvery few” example includes calls to unmanaged resources, such as an SqlConnection object.

A point is a crutch that is the last desire to save something useful from this particular execution of code blocks.

If, however, literally nothing can be done regarding exclusion, I would say that your only choice is to ignore it and allow it to pop up or catch, register and reconstruct.

+4
source

You should only catch exceptions, you can handle it. That is, you should not have try / catch statements everywhere.

Regarding exception exceptions, you should throw an exception if there is no better option. If you can efficiently handle user input, then there is no reason to throw an exception in this case. Without knowing the specifics, I would suggest that in this case there is no need for an exception.

+6
source

Trey Nash, in his book Accelerated C #, recommends (as probably most) to program in a way that negates the use of try / catch branches. Most of the attempts / catches you see can be eliminated with better programming. There are cases when you need it, mainly when working with file descriptors, database connections, basically at any time when you need to use the using statement (which behind the covers is a try / finaly block) with a type that implements IDisposable. Any use of anything on the OS that is beyond your control, where something might go wrong, use the catch attempt.

And try not to use shared

try { //some exception thrown } catch (Exception ex) { } 

This is too general; do your best to deal with the more important exceptions that may occur, instead of all of them being included in the general catch statement.

+4
source

In general, I tend to catch exceptions, log them, reverse them, and then ultimately have some application-level exception handling that represents a common error for the user.

In certain cases, when you can know what causes an exception (for example, you find an exception indicating that the connection to the database server is not working), you will catch a specific exception and then generate the corresponding error message for the user.

If an incorrect user input throws an exception, then you have an error code and you need to confirm your entry.

+2
source

You should avoid using try / catch as it is very expensive. In most scenarios, you can perform the necessary check to eliminate exceptions.

Regarding collections or arrays, you can check how many elements you have in your collection to avoid errors.

The actual place where you could use try catch would be when reading a file or accessing a third-party resource.

When you catch an exception, write it down and try to find a way to avoid it.

Also, when re-throwing, use the code below to try {

  } catch (Exception ex) { throw; } 

Hope that answers your question.

+2
source

Try/Catch says on its own that you tried to do something, something happened (in our case, nothing good), and you try to catch an exception to do something with this information.

Moral: if you know there might be an exception, and if you know what you are going to do if that happens, use Try/Catch to develop your exception handling logic.

If this is not the case, it makes more sense to have try/finally (for example, when IO accesses a resource provided to provide resources to provide resources).

Hope this helps.

+1
source

I just thought that I would throw this into the mix too. Eric Lippert made a great post about this a few years ago:

http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx

0
source

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


All Articles