I have a program that I'm trying to make throw a certain exception. I've created a custom Exception class here:
import java.io.*;
public class ShapeException extends Exception
{
public ShapeException(String message)
{
super(message);
}
}
Here is the class where I try and implement the Exception:
import java.io.*;
public class Circle
{
private double radius;
public Circle(double inRadius )
{
if(inRadius < 0.0)
{
throw new ShapeException("Shape Exception Occurred...");
}
else
{
radius = inRadius;
}
}
public double getRadius()
{
return radius;
}
public void setRadius(double newRadius)
{
if(newRadius < 0.0)
{
throw new ShapeException("Shape Exception Occurred...");
}
else
{
radius = newRadius;
}
}
public double area()
{
return Math.PI * radius * radius;
}
public void stretchBy(double factor )
{
if(factor < 0.0)
{
throw new ShapeException("Shape Exception Occurred...");
}
else
{
radius = radius * factor;
}
}
public String toString()
{
return "Circle Radius: " + radius;
}
}
However, this will not compile and gives me errors telling me that my shape exception error must be caught or declared to be thrown. What am I doing wrong? Is this not declared?