Is try catch is using a good coding (exception handling)

Is using try catch is good experience in coding practice here is a detailed answer .

Best practice is that exception handling should never hide issues. This means that try-catch blocks should be extremely rare.

There are 3 circumstances were using a try-catch makes sense.

1. Always deal with known exceptions as low-down as you can. However, if you're expecting an exception it's usually better practice to test for it first. For instance parse, formatting and arithmetic exceptions are nearly always better handled by logic checks first, rather than a specific try-catch.

2. If you need to do something on an exception (for instance logging or roll back a transaction) then re-throw the exception.


3. Always deal with unknown exceptions as high-up as you can - the only code that should consume an exception and not re-throw it should be the UI or public API.
Tags: ,

Join Us!