• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why so much difference between two similar class?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot !I think you are right.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic