Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A doubt in exception handling

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends plz get me cleared of the following doubt in exception handling.

class a
{
public static void main(String args[])
{
int i=6;
try
{
int j;
j=6/0;
}
catch(Exception e)
{
e.printStackTrace();
}
catch(ArithmeticException s)
{
s.printStackTrace();
}
}
}
we know that this code will give an error since the Exception object e catches all types of exceptions,so the second catch becomes unreachable and leads to error.
my question is how does the Exception class being a super class is able to handle the exceptions of its subclasses,such as ArithmeticException and ArrayIndexOutOfBounds?
normally a super class does not have the knowledge of its subclasses.
plz mail the reply.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a performance question - moving to Java in General (beginner)...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an exception is thrown the JVM looks up the call stack for a catch clause. If the actual exception object is a instance of the type declared in the catch clause, it's caught. Otherwise the JVM looks up the stack for another catch clause. So the specific exception doesn't know about its super classes so much as it is a valid instance of the super class.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you know, that makes me wonder... if everything (except primitive types, of course) "is-a" Object, can you write a catch clause for an Object? if not, why not?
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From
JLS section �14.18

A catch clause must have exactly one parameter (which is called an exception parameter); the declared type of the exception parameter must be the class Throwable or a subclass of Throwable, or a compile-time error occurs.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, thanks Carol! i'm always putting off reading language specifications, for any language; they make boring, tedious study material for me, so i experiment and ask questions instead.

you've just saved me two trips to the boredom of the JLS; your reply incidentally also told me i can't use one single catch block for two different exceptions. i'd been wondering if i could. that i can't sounds inconvenient, but i guess i don't need to do that too very often...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Beck:
your reply incidentally also told me i can't use one single catch block for two different exceptions. i'd been wondering if i could. that i can't sounds inconvenient, but i guess i don't need to do that too very often...
The main reason to want to catch multiple exception types is to ignore them all equally -- not usually a good design decision. However, I wouldn't mind an option to catch multiple types and treat them as a common superclass to handle them all the same way.To do that in correct Java code would look like thisor
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, David! your first example is pretty much exactly what i'd like to do, were it possible. (if i simply wanted to ignore several exceptions equally, i'd just catch Exception and ignore it. ) i guess i'll go with the style of your third example, even if it looks clumsy; it's not the only part of Java syntax that looks clumsy in my eyes...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic