Results 1 to 1 of 1
- 02-17-2012, 08:31 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Exception passing from super to sub class
Im wondering is this the most efficient way to use exception handling. Ive got a Super class, location and 2 subclass’s called shop and person.
The location class throws a custom exception called lesson1exception, which is just a string informing the used of the exception.
However I notice the city class, which is my static main args class, where my try and catch is has to throw the exception as well but so do both my sub classes for me to get no error.
Is this normal, do I need to have every sub class constructor throwing the super class exception or is there a better way to do this?
Main class
My shop class constructorJava Code:public static void main(String[] args) throws lesson1exception { try { kell = new Shop("kell","noreth"); } catch (lesson1exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
My super location classJava Code:public class Shop extends location { private String name; public int age=0; //this is my constructor sets the name and the location name public Shop(String string,String l) throws lesson1exception { // TODO Auto-generated constructor stub //super calls the constructor for the location class //NOTE-i think this is a waste since location is created without verifing validity first super(l); name=string; }
Java Code:public class location { protected String locationname; //this is where i throw my exception for location error public location(String l) throws lesson1exception{ if(l=="north"||l=="south"||l=="east"||l=="west") this.locationname=l; else throw new lesson1exception(l); }
Similar Threads
-
Gathering Data from a super class from a sub class
By kammce in forum New To JavaReplies: 2Last Post: 08-17-2011, 08:09 AM -
Referring to a super class
By blug in forum New To JavaReplies: 7Last Post: 03-20-2011, 05:06 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
super instanceof Class?
By mikeiz404 in forum New To JavaReplies: 11Last Post: 01-23-2009, 07:23 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks