HI, I was wondering what your thoughts are on exception handling, i.e I have a method:
public void Method{}
{
for (int i=0;i < length )
{
// dosomething that may case exception
...
...
// rest of the code
}
}
Should I add the try catch block for exception handling for the whole loop or just the code that is most vunerable or something else? What's the best practice?
The answer is on what level you want/can handle it. If processing of one element can fail but you can continue processing then use try catch inside loop. if error can happen and you can't continue then use outer try catch.
It depends on how you want your code to flow.
For example should the loop continue to execute even if one element throws an exception? If so, then you want your try/catch inside the for. If not then you want your try / catch around the for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With