Reputation: 335
I have a class, Line
, which takes two points, given as vectors, as a parameter and models the infinite line passing between them. A second class, BoundedLine
, takes two points and models the finite line connecting them.
An exception is thrown by Line
if the two points are the same, meaning that the call to the super constructor in BoundedLine
needs to be wrapped in a try catch block. Unfortunately it seems that the parameters are not available inside the try block; how would I access them?
// Constructor in Line
public Line (Vector start, Vector end) throws Exception {
if (start.equals (end)) {
throw new Exception ( "Points are the same" );
}
else {
this.start = start;
this.end = end;
modelLine (start, end);
}
}
// Constructor in BoundedLine
public BoundedLine (Vector start, Vector end) throws Exception {
try {
super (start, end);
}
catch (Exception e) {
throw e;
}
applyBoundaries (start, end);
}
I'm getting a compile time error of: "constructor Line in class Line cannot be applied to given types; required: Vector,Vector; found: no arguments; reason: actual and formal argument lists differ in length".
If I remove the exceptions and the try/catch block, then the code works fine.
Upvotes: 0
Views: 2460
Reputation: 635
All Java objects that don't have an explicit constructor defined have an implicit no-arg constructor which is called before any sub-class constructor.
The problem is that, as the call to the super constructor is not the first statement in the subclass constructor, the JVM is trying to call the default no-arg constructor of the superclass, which doesn't exist as you've created another constructor in the superclass.
The easiest thing to do, as the others have said, is to remove the try-catch - it's not buying you anything if you're just re-throwing the exception.
Upvotes: 1
Reputation: 6276
Emma, as the exception states that Constructor call must be the first statement in a constructor means that base constructor is to be invoked on the first line and no other statement can be placed before the base constructor.
Why this behaviour, it is interesting to know and your example also explains why no such statement can proceed the base class constructor.
In your example, if you were able to put try statement before super then you would have caught the exception and done nothing and object of BoundedLine would have been created but since we know the exception has been thrown from base class constructor thus all the properties, values of base object have not been created thus the object of derived class should also have not initialized..
Hope this explains the reason why no line or code can proceed the base class construtors.
Upvotes: 1
Reputation: 61158
If you declare that your BoundedLine
constructor throws Exception
there is no need to catch.
In any case the call to the superclass constructor must be the first line in a subclass constructor.
Try this:
public BoundedLine (Vector start, Vector end) throws Exception {
super (start, end);
applyBoundaries (start, end);
}
I would also add that throwing Exception
is a big no-no as you should throw a specific exception. There is an unchecked IllegalArgumentException
already in the JDK which you could use. If you want a checked exception I would recommend that you create your own.
public Line (final Vector start, final Vector end) {
if (start.equals (end)) {
throw new IllegalArgumentException( "Points are the same" );
}
this.start = start;
this.end = end;
modelLine (start, end);
}
Further you are using Vector
which is
List
. Please read about CollectionsUpvotes: 2
Reputation: 14269
If a call to super fails, the Object is not properly created. This would cause the entire Object to be theoretically undefined (if i.E. out of memory), therefore this is not allowed in Java.
Upvotes: 1
Reputation: 745
You can remove the try and catch block as you use BoundedLine() and if any exception occurs it will eventually be picked by throws statement..and you can mention catch the exception where you calling the function.
Hope its useful.
Upvotes: 1
Reputation: 271
Why do you catch Exception and throw it again? Just remove the try catch block.
Upvotes: 1