How to continue program execution even after throwing exception?

前端 未结 5 1349
盖世英雄少女心
盖世英雄少女心 2021-02-05 23:56

I have a requirement where in program execution flow should continue even after throwing an exception.

for(DataSource source : dataSources) {
    try {
        /         


        
相关标签:
5条回答
  • 2021-02-06 00:09

    The logic you have there now does exactly that. If the exception puts DataSource into an invalid state, the next attempt to use the iterator may throw a different exception, but that's a different matter and something specific to DataSource (so you'd have to look at whether DataSource lets you do something to deal with that other invalid state in some way). But in the general case, you're doing it right.

    0 讨论(0)
  • 2021-02-06 00:12

    No, there is no language support for handling an exception and then jumping back to the statement that caused it. You have to wrap separate try...catch constructs around every subsequence of operations that should execute even if the previous subsequence threw an exception.

    0 讨论(0)
  • 2021-02-06 00:15

    If an exception occurs on the for(DataSource source : dataSources) while calling dataSources to initialize the iterator, then that exception will not be caught.

    Is that what you mean by "If exception is thrown in the first iteration, flow execution is stopped."?

    If this is the case, then there's nothing else you can do and the loop should not execute any interations.

    0 讨论(0)
  • 2021-02-06 00:29

    If you are not breaking the loop somehow inside the catch block, then the other iterations will just continue, regardless of whether an exception was thrown in a previous iteration.

    Try this simple example and see what happens:

    List<String> list = new ArrayList<String>();
    list.add("1");
    list.add("2");
    list.add("3");
    
    for(String str: list) {
       try {
           System.out.println(str);
           throw new Exception("Exception for string " + str);
       } catch(Exception ex) {
           System.out.println("Caught exception");
       }
    }
    

    You will see that all iteration execute, even though each one throws an exception.

    0 讨论(0)
  • 2021-02-06 00:32

    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.

    0 讨论(0)
提交回复
热议问题