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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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.
-
Recent
- Differences between abstract Class and Interface
- What it means if you don’t override equals()
- Handling Exceptions
- Unreachable code
- Check Java Heap Size and Free Memory
- Switch your domain to SIP
- AES health checker script
- User Management
- EXPLAIN
- Obtain the table storage format
- Deleting Old Log Files
- PROFILER and EXPLAIN
-
Links
-
Archives
- June 2013 (2)
- May 2013 (2)
- May 2012 (1)
- February 2012 (1)
- September 2011 (1)
- August 2011 (3)
- June 2011 (4)
- May 2011 (4)
-
Categories
-
RSS
Entries RSS
Comments RSS