wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes A doubt in exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A doubt in exception handling" Watch "A doubt in exception handling" New topic
Author

A doubt in exception handling

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
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

Joined: Jul 11, 2001
Posts: 14112
Not a performance question - moving to Java in General (beginner)...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Jan 14, 2005
Posts: 323
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jan 14, 2005
Posts: 323
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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A doubt in exception handling
 
Similar Threads
doubt!!
Doubt in Exceptions
Doubt
A doubt in exception handling
A doubt in exception handling