How to continue program execution even after throwing exception?
java
Solution
Well first of all ,
There are 2 types of Exceptions. Checked & Unchecked.
Unchecked exceptions are the ones that your program cannot recover from. Like NullPointers, telling you that something is really wrong with your logic.
Checked exceptions are runtime exceptions, and from these ones you can recover from.
Therefore you should avoid using catch statemens looking for the "Exception" base class. Which are represent both times. You should probably consider looking for specific exceptions(normally sub-classes of Run-Time exceptions).
In short, there is much more into that.
You should also keep in mind that you shouldn't use exception handling as workflow. usually indicates that your architecture is somehow deficient. And as the name states, they should be treated as "exceptions" to a normal execution.
Now, considering you code :
for(DataSource source : dataSources) {
try {
//do something with 'source'
} catch (Exception e) { // catch any exception
continue; // will just skip this iteration and jump to the next
}
//other stuff ?
}
As it is, it should catch the exception and move on. Maybe there is something your not telling us ? :P
Anyway, hope this helps.
Problem
I have a requirement where in program execution flow should continue even after throwing an exception. ``` for(DataSource source : dataSources) { try { //do something with 'source' } catch (Exception e) { } } ``` If exception is thrown in the first iteration, flow execution is stopped. My requirement is even after throwing exception for the first iteration, other iterations should continue. Can i write logic in catch block?