Reputation: 523
What is the problem here? I am just simply trying to catch a custom exception depending on a if statement in my constructor, the entire code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package question3_test;
/**
*
* @author jackandjill
*/
public class Triangle2
{
private int side1, side2, side3;
private double area;
private String message;
public int getSide1()
{
return side1;
}
public void setSide1(int s1)
{
side1 = s1;
}
public int getSide2()
{
return side2;
}
public void setSide2(int s2)
{
side2 = s2;
}
public int getSide3()
{
return side3;
}
public void setSide3(int s3)
{
side3 = s3;
}
public Triangle2(int a,int b,int c) throws InvalidValueException
{
setSide1(a);
setSide2(b);
setSide3(c);
try
{
if (!(side1 + side2 > side3) && (side2 + side3 > side1)&& (side1 + side3 > side2))
throw new InvalidValueException("Invalid Value");
}
catch (InvalidValueException excep)
{
message = excep.getMessage();
//throw new InvalidValueException(message);
}
public double findArea(int side_1, int side_2, int side_3)
{
int s, a, b,c;
a = side_1;
b = side_2;
c = side_3;
s = ((a + b + c)/2);
area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
//area =
return area;
}
}
}
The problem is at my catch statement, it is underlined in red and says never thrown in the corresponding try statement
Upvotes: 1
Views: 319
Reputation: 213223
You can't throw an Exception class like in the below code: -
throw InvalidValueException;
You have to make a new instance of your exception, and then throw it: -
throw new InvalidValueException();
But, still there is some logic problem in your code. You have declared your Exception
to be throws, but at the same time, you are handling it in the catch
block itself. Why would you do that?
UPDATE: -
As per your modified post: -
try
{
if (!(side1 + side2 > side3) && (side2 + side3 > side1) &&
(side1 + side3 > side2))
message = "Invalid Triangle";
} catch (InvalidValueException e) {
message = "Invalid Traingle";
}
Since you are no-where throwing any Exception in your try block
, the catch block
will complain about that..
If you want to throw
an exception and catch
it in place, use it like this: -
public Triangle2(int a,int b,int c) throws InvalidValueException
if (!(side1 + side2 > side3) && (side2 + side3 > side1) &&
(side1 + side3 > side2))
throw new InvalidValueException("Invalid Value")
}
And suppose you are using it from your main
method, you should have a try-catch
block there, to handle the declared exception in constructor.: -
public static void main(String[] args) {
Triangle triangle = null;
try {
triangle = new Triangle(2, 3, 4);
} catch (InvalidValueException e) {
e.printStackTrace();
}
}
If you handle the exception in your method only, you should not declare it in your method declaration.. But if you are not handling it, then you must declare that exception to be thrown.
An exception should be either handled, or declared to be thrown (In which case, you should actually throw that exception), but never both
See This tutorial for more about Exception: - http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html
Upvotes: 2
Reputation: 51030
Looks like InvalidValueException
is a checked exception (compiletime exception) and your code within the catch block doesn't throw that exception.
You can overcome that error message (in a meaningful way) by first checking the values in your method and immediately throwing the exception if any of them are not valid.
Or, if possible, you can also make it a RuntimeException
instead.
Upvotes: 0
Reputation: 200148
There is some confusion in your code in the way you use exceptions.
try
block you never throw your exception, but try to catch it below.public Triangle2(int a,int b,int c)
throws InvalidValueException // declared, but never thrown!
{
try
{
if (!(side1 + side2 > side3) && (side2 + side3 > side1)&& (side1 + side3 > side2))
message = "Invalid Triangle";
}
catch (InvalidValueException excep) // caaught, but never thrown in try body!
{
message = "Invalid Triangle";
}
}
This is the way you should write it, much simpler in fact:
public Triangle2(int a,int b,int c) throws InvalidValueException
{
setSide1(a);
setSide2(b);
setSide3(c);
if (!(side1 + side2 > side3) && (side2 + side3 > side1)&& (side1 + side3 > side2))
throw new InvalidValueException("Invalid Triangle");
}
You are not supposed to catch the exception in the same context where you threw it: the place where you use the constructor, new Triangle2(a,b,c)
, is the (earliest sensible) place to catch.
Upvotes: 2
Reputation: 41200
You should re-throw your custom exception
from catch block .
catch (InvalidValueException excep)
{
message = "Invalid Triangle";
throw new InvalidValueException(message);
}
Upvotes: 0