• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Reg. exception subclass

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain the below with an example.


A method declaring that it throws a certain exception class may throw instances of any subclass
of that exception class


thanks.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this:
BallsException----base class of---->ScrewBallException.
ShootException----base class of---->FreeThrowException.
What this really means is that if a method "pitcher()" declares that it throws "BallsException", it is legal for it to throw "ScrewBallException". But it cannot throw a "ShootException" or "FreeThrowException" since their super class is not "BallsException".
eg :
void methodx() throws SQLException{
.......
throw new SQLException(); //legal
throw new BatchUpdateException(); //legal since it's an instance of a subclass of SQLException.
throw new AWTException(); //illegal , does not extend SQLException.
.......
}

------------------
MCP 2000, CCNA , SCJP 2
Webmaster - http://java.ditmas.net
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is better explained if you just look once at how you catch the Exception.
you start with the most specific one first than go on catching more generic one (child first than parent) cause
once you put the parents catch block, all child exception will get caught there itself.


The reverse happens while you throw exception (or you indicate in method declaration that you intend to throw any exception).
Method, which throws an exception, is also authorized to throw all its child exception as well.


Originally posted by Angela Narain:
Can someone explain the below with an example.
thanks.


 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get a compile time error if i have a method as below :
public static void methodA() throws Exception
{
throw new SubException();
throw new SuperException();
}
class SuperException extends Exception {}
class SubException extends SuperException {}

test.java:75: unreachable statement
throw new SuperException();
^
[This message has been edited by Angela Narain (edited September 19, 2001).]
 
Amit Agrawal
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a different compilation error. Try this..

(p.s. : this error will also come in the previous code given by me).

Originally posted by Angela Narain:
I get a compile time error if i have a method as below :
public static void methodA() throws Exception
{
throw new SubException();
throw new SuperException();
}
class SuperException extends Exception {}
class SubException extends SuperException {}

test.java:75: unreachable statement
throw new SuperException();
^
[This message has been edited by Angela Narain (edited September 19, 2001).]


 
reply
    Bookmark Topic Watch Topic
  • New Topic