A method performs several tasks:
public void method () {
try {
task1(); //may throw sql exception
task2(); //may throw sql exception
task3(); //may throw sql exception
} catch (SQLException se) {
//from which line cames the exception?
}
I’m looking for a criteria to adopt in these situations.
Currently my idea is this:
1) if i need to take some action depending on the specific line that has thrown the exception, the only thing is surrond each statement with his-own try-catch; three in the example.
2) if i not need to take a specific action based on the line that throws the exception, then the stack trace will give enough information to know line has gone wrong without a try-catch block for each instruction, that makes the code less readable.
The stacktrace will contain all the necessary information.
If you need to take specific actions, then in each
taskX()method you may want to throw a different exception, wrappingSQLException. Same verbosity though.