• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding exceptions help  RSS feed

 
kieran pattni
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
class ExceptionApp1
{
public void doSomething() throws RuntimeException

{
System.out.println("I am doing something here!");
}
}

class ExceptionclassA extends ExceptionApp
{
public void doSomething() throws FileNotFoundException,
EOFException,
SecurityException
{
System.out.println("I am doing something here!");
}
}

class ExceptionclassB extends ExceptionApp
{
public void doSomething() throws FileNotFoundException,
EOFException,
SecurityException,
ClassNotFoundException
{
System.out.println("I am doing something here!");
}
}

hi...i known what checked exception is and unchecked exceptions..but i understand why does the above compiles..i should get complier error where there is ClassNotFoundException in the overriding doSomething();..i am using jCreator...
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ExceptionclassA and ExceptionclassB are extending ExceptionApp whose code is not provided here. But incase you meant both are extending ExceptionApp1, compilation error will occur in Eclipse 3.1
[ January 25, 2006: Message edited by: karthi keyan ]
 
kieran pattni
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ...overriding methods with exceptions...i dont get complier error when i have ClassNotFoundException in third class method...i know u cant throw new exceptions..but havent i just done that...thanks for help..still confused
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The basic thing to keep in mind is "no surprises". An overridden method can't list a checked exception in its throws list which isn't present in the throw list of the method it is overriding, either exactly, or by the listing a super class.

In class Good:
  • a -- the base method and the overriden method list no exceptions thrown, so this is OK.
  • b -- the base method and the overriden method both specify the same exception, IOException, so this is OK, too.
  • c -- the overridden method lists fewer exceptions: none versus IOException, so this is OK.
  • d -- the exceptions listed in the overridden method, FileNotFoundException and MalformedURLException, are subtypes of the exception class listed in base.d - IOException - so this is OK.
  • e -- adds IllegalStateException, but you are allowed to freely add unchecked exceptions at any time.

  • In class Bad:
  • a -- A checked exception (IOException) is added -- not allowed
  • b -- A checked exception (SQLException) is added -- not allowed
  • c -- A more general exception class is listed (Exception, versus IOException) -- not allowed

  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!