Question: How to do both: handle exception in outer
method and return result of inner
method?
I have: two methods which return List
:
import java.util.List;
import java.util.LinkedList;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Result = " + new HelloWorld().parseWrapper());
}
public List<Integer> inner() {
List<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < 5; i++) {
if (i % 4 == 0) throw new RuntimeException();
list.add(i);
}
return list;
}
public List<Integer> outer() {
List<Integer> list = null;
try {
list = parse();
} catch (Exception e) {
System.out.println("Handle exception!");
} finally {
return list;
}
}
}
Result:
Handle exception!
Result = null // PROBLEM: I DON'T WANT TO LOOSE IT
Problem: I loose result list. I want both: to handle exception and to return [1, 2, 3] list from outer
method.