wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception doubt" Watch "Exception doubt" New topic
Author

Exception doubt

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
1.Compiler error complaining about the catch block where no IOException object can ever be thrown.
2.Compiler error - IOException not found. It must be imported in the first line of the code.
3.No compiler error. The lines "Before Try" and "At the end" are printed on the screen.

Answer is given as 1.
If the body of try block does not contain any code, can't we catch any exception?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
How would you cause an exception to be thrown if there isn't any code?
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
If I replace IOexception with Throwable, the code does not give any compilation error.howz that possible?

Here is the code.

public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Because all unchecked exceptions are subclasses of Throwable.
Antonio Trapero
Greenhorn

Joined: Mar 24, 2007
Posts: 11
If you dont throw any exception inside try/catch body , nor a call to a method that throws the exception, you have a compilation error. If you catch Throwable or error, it let you compile, cause errors could happen any time in runtime (you dont declare it).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception doubt