0

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?

Abbey S
  • 111
  • 2
  • 15
  • https://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html – shmosel Nov 21 '16 at 22:35
  • 2
    Read the errors. Google them if you have to. The compiler is not just having a bad day and picking on you. They mean something! `shape exception error must be caught or declared to be thrown` – John3136 Nov 21 '16 at 22:35

2 Answers2

1

In Java there are two types of Exception. The one you are using is called Checked Exception, which is used for recoverable errors. When Checked Exception is thrown you have to handle it. Either you handle it where it occurs with a try-catch block.

try {
    methodThrowingShapeException()
} catch (ShapeException e) {
    // Log and handle the Exception
}

Never leave catch block empty!

Or you can declare it to be thrown in method signature in which case exception has to be handled by caller of the method.

public void setRadius(double newRadius) throws ShapeException
Community
  • 1
  • 1
Januson
  • 4,533
  • 34
  • 42
1

In java whenever you throw a checked exception you need to declare it in the method signature with throws keyword. Below is the code snippet which are not having any compilation errors because each method has throws declaration wherever ShapeException is thrown.

public class Circle {
    private double radius;

    public Circle(double inRadius) throws ShapeException {
        if(inRadius < 0.0) {
            throw new ShapeException("Shape Exception Occurred...");
        } else {
            radius = inRadius;
        }
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double newRadius) throws ShapeException {
        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) throws ShapeException {
        if(factor < 0.0) {
            throw new ShapeException("Shape Exception Occurred...");
        } else {
            radius = radius * factor;
        }
    }

    @Override
    public String toString() {
        return "Circle Radius: " + radius;
    }
}
kk.
  • 3,747
  • 12
  • 36
  • 67