• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Why so much difference between two similar class?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read following code,why the code1 compile while the code2 not.Give me something about the item.
code1:
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");
}
}
code2:
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");
}
}
the code1 ans :3(No compiler error. The lines "Before Try" and "At the end" are printed on the screen. )
the code2 ans :1(Compiler error complaining about the catch block where no IOException object can ever be thrown. )
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
The first example passes the compile ok as all exceptions, runtime and checked, extend Throwable. Since a runtime exception can be thrown any time, anywhere, there is no problem.
In the second example, IOException is a checked exception. The compiler can tell from the code that there is no possibility that one will be thrown and coughs up the error.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, in code 1, you are trying to catch Throwable, which includes both Exception and Error. And it is possible that during the

there are some Error or RuntimeException(which are indeed, unchecked exception) happens, that is the reason compiles let you go with the compiling.
But with code 2, you are trying to catch <code>IOException</code>, which is a checked exception, the compiler definitely knows that it will not happen during

and as a result, failed the compiling.
Correct me if i am wrong.
Guoqiao
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot !I think you are right.
 
Everybody's invited. Even this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic