• 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

Reg. Checked exceptions

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

class MyException extends Exception {}
class SupException extends MyException {}
class SubException extends MyException {}
public class test
{
public static void main( String[] args )
{

}
public void m() throws MyException , SubException {}
}
The above code compiles fine.
Is there any use of declaring a super class exception and a
sub class exception together as m() is doing ?
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Angela, I think maybe sometimes we want to emphasize some of the sub exception in the declaration for more attention, so we declare the exception together with some of its exception which typically happens. That is my understanding.
Regards,

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there's any use in doing so. If the general exception preceeds the specific one, the specific one will be ignored. Furthermore, Khalid Mughal's book denounces this as poor programming practice.
Also, doing so is not a compile time error unlike in the catch block where it is a compile time error to catch the general exception before the specific one (its subclass).
Correct me if I am wrong.
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 12, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note Shyamsudar that no catch was used in the example, but you are talking about the order of the possible catch clauses to deal succesfully with a situation where both exceptions could be thrown.
I mean Guoqiao addressed exactly the question. Yes it is useful to remember what you said, but it is not a poor programming practise if you put the cathc clauses rigthly ordered to trap firtly the sub Exception. And as the example shows no cacth clause at all you have assumed an incorrect use of them. Again this was not the question.
But it was useful to remmember it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic