Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

exception handling impl

Posted on 2011-05-04
3
Medium Priority
?
279 Views
Last Modified: 2012-05-11
I was reading folowing lines and I did not understand clearly.
The overriding method can throw narrower or fewer exceptions. Just because
an overridden method takes risks doesnot mean that the overriding subclass
exception takes the same risks. Bottom line is overriding method doesnot have to declare any exceptions that it will never throw, regardless of what the
overridden method declares.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 35695164
This states that overriding method is not required to throw the same exceptions as overriden method.
Overriding method can be different and therfore may not run some risks which overriden method runs.
So there is no strict relations ship betwenn the thrown exceptions as there is strict
relationship betwenn signatiures of the overriding and overriden methods
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35695172
This is becasue overriding has in fact to do with the way method interacts with the outside world - therefore
signatueres shoudl be the same, and exceptions very much depend on implementation.
Maybe ovrriden method goes to get the result to some file, and
overriding does nit need to do it then obviously it would not need to throw those exception which overriden method needs to throw
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 35695176
An overriding method does not have to declare it throws the same exception as the one it is overriding (or even declare any exception).
But any exception it does declare it throws needs to be a subclass of whats defined in parent class
public class Base {
   public x() throws IOException {
      ...
   }
}

// ok

public class A extends Base {
   public x() throws IOException {
      ...
   }
}


// ok

public class A extends Base {
   public x() {
      ...
   }
}


// ok

public class A extends Base {
   public x() throws FileNotFoundException {   // subclass of IOException
      ...
   }
}


// *not* ok

public class A extends Base {
   public x() throws AWTException {
      ...
   }
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question