sabaTEL.net

Java, MySQL, Avaya

Unreachable code

A typical question that we can expect at the exam is given a code that has no errors, try simulate the execution to find out what will be the score.


public abstract class A26D{
public abstract void start() throws ArithmeticException;
public static void main(String args[]){
A26D a=new A26D(){
public void start() throws ArithmeticException{
System.out.print(1);
try{
System.out.print(2);
throw new ArithmeticException();
System.out.print(3);
}catch(Exception e){
System.out.print(4);
}finally{
System.out.print(5);
}
}
};
a.start();
}
}

view raw

A26D.java

hosted with ❤ by GitHub

In those kind of questions, you don’t expect (at least I wasn’t expecting) the compiler to complain about a condition that can never be satisfied in order to execute certain code.

In this example, the fact that we are throwing an exception at the try block means that line 10 will never be executed. The Exception will be catch but we will never return to finish the rest of the try block.

May 24, 2013 Posted by | Java | Leave a comment