Need help understanding code (Exception Handling)
class Levels {
static public class Ex1 extends Exception {}
static public class Ex2 extends Exception {}
static public void main(String[] args)
{
try {
try {
for (int i = 0; i < 3; i++) {
System.out.println("Looping...");
level1();
}
} catch (Ex1 ex) {
throw new Exception("123");
} catch (Ex2 ex) {
throw new Exception("ABC");
} catch (Exception ex) {
throw new Exception("ABC123");
} finally {
System.out.println("Foo");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
System.out.println("Bar");
}
}
static private void level1() throws Exception
{
System.out.println("Entering Level 1");
level2();
System.out.println("Leaving Level 1");
}
static private void level2() throws Exception
{
System.out.println("Entering Level 2");
level3();
System.out.println("Leaving Level 2");
}
static private void level3() throws Exception
{
throw new Ex2();
}
}
Output -
Looping...
Entering Level 1
Entering Level 2
Foo
ABC
Bar
I'm confused after Entering Level 2. Not sure how it gets to Foo and onwards.
static private void level3() throws Exception
{
throw new Ex2();
}
Might be cause I don't understand "throw", "throws", "extends" and "new". Can someone explain it in simplest term possible.
Posted Feb 22, 2016, 3:58 AM UTC
12