• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

When we use the try catch module like this

try
{
some code

}
catch ( baseclass)
{
//some code
}
catch (This exception is a subclass of previous catch exception class)
{
//some code
}
I get an error .But since a base class has less functionalities than a subclass it should run ? Am I right??

Thanks
Regards
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing (since you don't say) that your error is a compilation error, it is something along the lines of "Unreachable statement" and it applies to the second catch statement?

Suppose an exception of your sub-type is raised in the try block. It inherits from the base-class so is a base-class type itself. And thats why you can never get to the second catch statement.
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But since a subclass has a higher functionality than a base class which means that subclass objects may be having better way of dealing with exceptions than base class so should it not inherit from subclass?
Why inherit from a base class when subclass is there?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kartik Mahadevan:
But since a subclass has a higher functionality than a base class which means that subclass objects may be having better way of dealing with exceptions than base class so should it not inherit from subclass?
Why inherit from a base class when subclass is there?



"higher functionality"? Exception classes pretty much all have the same funtionality (as defined by the interface they all implement java.lang.Throwable). You might subclass an exception to provide more description about the problem. Look at the subclasses for IOException for example, there a set of exceptions which all describe more specific IO problems (EOF, Socket problems, Unknown Host etc.). How you handle those is up to you. You might just want to know know that the IO operation failed, in which case catch the super class IOException. Alternatively it might be expedient to have your application respond differently if the IO operation fails in a specific way, in which case you would catch the sub class of IOException first, and IOException second. Make sense?
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kartik Mahadevan:
But since a subclass has a higher functionality than a base class which means that subclass objects may be having better way of dealing with exceptions than base class so should it not inherit from subclass?
Why inherit from a base class when subclass is there?



I think you may just misunderstand how the catch block(s) work. The catch blocks are searched in order from top to bottom for the first one that catches a type that's "assignable from" the type of exception that's thrown. It does not just pick the "best" match. Your example should cause a compile error, stating (as someone else pointed out) that your second catch block is "unreachable" since it's exception type is a subclass of the first catch block's exception type.
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir
I am still not able to get it

Thanks
Regards
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir

If a subclass catch is used at the top then surely it should catch an exception which a base class would catch because all the functionalities
of the base class would be present in the subclass.So in this case the exception would be resolved in the first case(subclass catch) itself
and thus the next baseclass catch would not run.
( Assuming that subclass catch is followed by baseclass catch)
then even in this case the base class is unreacheable.
I hope i could properlyexplain my problem.

Thanks
Regards
Kartik
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think how "catch" might work - each catch statament is checking the Exception it gets, and checking it against the named Exception you have asked it to look out for. If it matches, it handles it. If you put the most general exception in the first catch statement, nothing will get past it, because all exceptions thrown in the try block will be of that type.

Look at this example:

OK its not very useful (and may not even compile - since I haven't tried). The connectToURL() method throws three Exceptions, all of which are of the type IOException (i.e. they all extends that class). In my program I don't want to have to catch each possible exception individually - since only one has any real meaning where I am concerned. So I catch the one I want to do something different with (UnknownHostException), and handle it. All the other possible exceptions are caught by the second catch statement.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not exactly. If you do a try catch block like...



The IOException block can only catch subclasses of IOException. But, the Exception catch block can catch ANY exception thrown! You've got it the other way around. The internal functionality of the class has nothing to do with this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic