package net.dharmaraj;
public class ThrowsDemo
{
static void throwMethod()
{
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
A) Compilation error
B) Runtime error
C) Compile successfully, nothing is printed.
D) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo
Compile Result :--
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for IllegalAccessException. This exception is never thrown from the try statement body
at net.dharmaraj.ThrowsDemo.main(ThrowsDemo.java:17)
Changed Code :--
package net.dharmaraj;
public class ThrowsDemo
{
static void throwMethod() throws IllegalAccessException
{
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
Out Pur:--
Inside throwMethod.
Caught java.lang.IllegalAccessException: demo
No comments:
Post a Comment