0

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.

4

6 回答 6

2

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

  1. A rawtype - please read about Generics
  2. An obsoleted collection type, use a List. Please read about Collections
于 2013-10-15T12:09:29.007 回答
1

Why do you catch Exception and throw it again? Just remove the try catch block.

于 2013-10-15T12:08:45.703 回答
1

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.

于 2013-10-15T12:11:16.843 回答
1

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.

于 2013-10-15T12:11:50.847 回答
1

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.

于 2013-10-15T12:12:35.063 回答
1

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.

于 2013-10-15T12:18:10.457 回答