No, they are different
Case 1 public class ExceptionTest {
public static void main(
String args[])
{
try
{
System.out.println(2/0); //
Line 1 throws an ArithmeticException. Compiler dont know for sure if an exception can occur as code in the try block may cause an exception. The next println is not executed and control runs to the nearest catch that matches and the finally is executed. It doesnt show any compiler error because you are handling you error. Even if you dont handle it wont show compiler error but an runtime exception will occur. System.out.println("try block");
}
catch(Exception e)
{
System.out.println("catch block");
}
finally
{
System.out.println("finally block");
}
}
}
Case 2 public class ExceptionTest {
public static void main(String args[])
{
try
{
throws new ArithmeticException(); //
here you are throwing an exception object so surely the next line wont be executed so compiler error as it become unreachable. System.out.println("try block");
}
catch(Exception e)
{
System.out.println("catch block");
}
finally
{
System.out.println("finally block");
}
}
}
[ September 28, 2008: Message edited by: vidhya suvarna ]