这里有什么问题?我只是试图根据构造函数中的 if 语句捕获自定义异常,整个代码
/*
* 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;
}
}
}
问题出在我的 catch 语句中,它带有红色下划线,并在相应的 try 语句中表示从未抛出