This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Exception Question  RSS feed

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:
public class MQ1
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t) //1
//catch(Exception t) //2
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
As //1 showed,this code won't compile with error that no IOException is thrown in try block.
But if //1 is replaced with //2
it compiles without problem.
I can not understand what is behind this.
Please clear it for me
THX
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can catch unchecked exception without even throwing in the try block, but there must be some code that throws Checked exception to catch checked exception. Pl correct me if I'm wrong.
Santhosh.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The Exceptions that are declared in a try-catch() block should be of type, java.lang.Exception or java.lang.Throwable or java.lang.Error.
Exception hierarchy:
java.lang.Object
|
+--java.lang.Throwable
|
+--java.lang.Exception
|
+--java.io.IOException
Direct Known Subclasses:
Ex: java.io.EOFException
As you can see java.io.IOException extends java.lang.Exception.
Hence your try-catch() block should be declared to catch exceptions of type Exception/Throwable.
java.io.IOException can be defined in a try-catch() block only if subclasses of java.io.IOException are thrown.
Example:
public class MQ1
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
throw new java.io.EOFException();
}
catch(java.io.IOException e1) //1
{
System.out.println("Inside IOException");
}
catch(Exception e2) //2
{
System.out.println("Inside Exception");
}

catch(Throwable e3) //2
{
System.out.println("Inside Throwable");
}
System.out.println("At the End");
}
}
The Output of the above code is:
Before Try
Inside IOException
At the End
- Suresh Selvaraj
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer lies in the fact that while 'Exception' is generic and could be anything (including exceptions like RuntimeException ?), 'IOException' applies to IO related exceptions specifically ? ... just a guess .. would be interested to know for sure

John

Originally posted by Bin Zhao:
Consider the following code:
public class MQ1
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t) //1
//catch(Exception t) //2
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
As //1 showed,this code won't compile with error that no IOException is thrown in try block.
But if //1 is replaced with //2
it compiles without problem.
I can not understand what is behind this.
Please clear it for me
THX


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!