Thought of writing some thing on exception and how it should be handled. These are some of the practices to be followed when managing exceptions in code to give you some more performance.
1) Do not use exceptions to control application logic. Throwing an exception is expensive (building the stack trace, etc are expensive). For an example when searching for a student and when it does not exist, do not throw a StudentNotFoundException, as this is a common scenario, return a boolean to indicate this or else another way is to return an enumerated type to denote error.
2) Use validation code to avoid exceptions, examples like checking for null etc..can avoid unnecessarily generating exceptions.
3) Use a “finally “ block to ensure resources are released (everyone knows that by default.)
4) Re-throwing exceptions with the “throw” keyword is expensive this is same as throwing a new exception.
5) Don’t catch exception that you can’t handle. The best way is to let it propagate up the call stack so a most suitable user can (part of the code that is...) handle it.
6) Do not catch general exception, this leads hard to debug when it’s time to do so !.
1) Do not use exceptions to control application logic. Throwing an exception is expensive (building the stack trace, etc are expensive). For an example when searching for a student and when it does not exist, do not throw a StudentNotFoundException, as this is a common scenario, return a boolean to indicate this or else another way is to return an enumerated type to denote error.
2) Use validation code to avoid exceptions, examples like checking for null etc..can avoid unnecessarily generating exceptions.
3) Use a “finally “ block to ensure resources are released (everyone knows that by default.)
4) Re-throwing exceptions with the “throw” keyword is expensive this is same as throwing a new exception.
5) Don’t catch exception that you can’t handle. The best way is to let it propagate up the call stack so a most suitable user can (part of the code that is...) handle it.
6) Do not catch general exception, this leads hard to debug when it’s time to do so !.
Comments
Post a Comment