The following code is for Questions 81 and 82
//Contents of File AnInterface.java
public interface AnInterface
{
public void methodOne() throws Exception;
}
//Contents of File AnInterfaceImpl.java
public class AnInterfaceImpl implements AnInterface
{
public void methodOne()
{
System.out.println("I will never throw an exception");
}
}
Question 81.
Read the code below carefully.
public class ATest
{
public static void main(
String args[])
{
AnInterface ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
Will cause no compile time error and print "I will never throw and Exception the screen".
Will Cause a run time error .
--------------------------------------------------------------------------------
Question 82.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
Will cause no compile time error and print "I will never throw and Exception the screen".
Will Cause a run time error .
The answer for Question 82 is: "Will cause no compile time error and print "I will never throw and Exception the screen".
The answer for Question 81 is: "Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String)) "
I agree with the answer of Question 82. I do not understand why does the code in Question 81 throw an error[I thought it will compile].
Any comments will be appreciated.