#Exceptions

61 messages · Page 1 of 1 (latest)

round flare
#

Hey I am learning exceptions and this is what I am tasked. I have the following code, I believe I have done this right I just dont quite comprehend what I am doing

In a triangle, the sum of any two sides is greater than the other side. The Triangle2 class must adhere to this rule. Create the custom IllegalTriangleException class, and modify the constructor of the Triangle2 class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:
/** Construct a triangle with the specified sides */
public Triangle2(double side1, double side2, double side3) throws IllegalTriangleException
{
// Implement it
            }

 You will make a custom exception class, IllegalTriangleException.  See Listing 12.10, pg. 470, for an example. TestTriangle.java has been provided that has a main that you will need to add exception handling code. You will need to detect and throw the exception the constructor for class Triangle2.   Triangle2 is already started in TestTriangle.java. The class is named Triangle2 to distinguish from the Triangle class from an earlier assignment. 

public class TestTriangle {
    public static void main(String[] args) throws IllegalTriangleException {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three sides: ");
        double side1 = input.nextDouble();
        double side2 = input.nextDouble();
        double side3 = input.nextDouble();

        Triangle2 triangle = new Triangle2(side1, side2, side3);


        System.out.println("The area is " + triangle.getArea());
        System.out.println("The perimeter is "
                + triangle.getPerimeter());
        System.out.println(triangle);

    }
}

class Triangle2  {
    private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

    /** Constructor */
    public Triangle2() {
    }

    /** Constructor */
    public Triangle2(double side1, double side2, double side3)throws IllegalTriangleException {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
plain hamletBOT
#

Hey, @round flare!
Please remember to /close this post once your question has been answered!

round flare
#
public class IllegalTriangleException extends Throwable {

}
#

like in this class I am unsure what to do

ruby marsh
round flare
#
throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:
/** Construct a triangle with the specified sides */
public Triangle2(double side1, double side2, double side3) throws IllegalTriangleException
{
// Implement it
            }
#

this is what I need to do

round flare
#

my biggest trip up is how

#

liek how would I check a constructor?

#

would it not failr

ruby marsh
#

However on main this isn’t very sensible. You catch exception by invoking the method within a try catch. This is not possible with main for obvious reasons

round flare
#

why would it not be?

ruby marsh
round flare
#

so like this?

#

try{
Triangle2 triangle = new Triangle2(side1, side2, side3);
}
catch (IllegalTriangleException e){

    }
ruby marsh
round flare
#

but what I am confused is I have the try catch

#

I am checking

#

see if it works etc, but like what does my error do

#

like the class I am making, how am I making it

#

like rn it does nothing

#

whats it checking ya know?

#

so like it tries, if it works it works, else it catches and then what

#

IllegalTriangle does what?

#

if it doesnt work it just wouldnt work no?

ruby marsh
ruby marsh
round flare
#

so in
public class IllegalTriangleException extends Throwable {

}
I just write wrong shit?

#

like a sout of there was an error?

ruby marsh
#

no

round flare
#

thats whats confusing me the most is whats the point of calling a class

ruby marsh
#

also why are you subclassing throwable

round flare
#

wym?

#

the extends ?

#

IDE created error

#

when its without

ruby marsh
#

ohhh on the IllegalTriangleException it’s because you’re making a custom exception i.E. throwable yes you need to do that

round flare
#

I thought I was doin that right 😂

#

had me worried

ruby marsh
#

eh although

#

extend Exception instead

round flare
#

what do you mean?

#

like extend via constructor?

ruby marsh
#

You don’t extend anything in the constructor

#

Change the signature of the exception class to replace throwable with exception

round flare
#

uh could you show an example?

#

a little confusing sorry

ruby marsh
#

public class IllegalTriangeException extends Exception

round flare
#

oh I see

#

try{
Triangle2 triangle = new Triangle2(side1, side2, side3);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is "
+ triangle.getPerimeter());
System.out.println(triangle);
}
catch (IllegalTriangleException e){

    }
#

so instead of e

#

Exception too

ruby marsh
#

nah

#

that’s just the name of the exception

#

you also still need to actually throw them btw

round flare
#

what do you mean

#

like call the class inside the catch