# Need help creating my own exception for circle class

• 01-30-2013, 09:01 PM
ndsmith20
Need help creating my own exception for circle class
I need to create a custom exception for my circle class to replace the illegal argument exception, im getting tons of errors in my custom exception class and honestly I am lost, help would be greatly appreciated:

Code:

```public class Circle {     private double radius;    // the radius of the circle     /**     * Constructor - Create a new circle.     *     * @param    inRadius  radius of the circle           *throw illegal argument exception if inRadius less than or equal to 0     */     public Circle(double inRadius ) {         if (inRadius <= 0.0)                   {                           throw new IllegalArgumentException("Radius must be greater than 0");                   }                   radius = inRadius;                       }     /**     * Return the radius of the circle.     *     * @return    radius of the circle     */     public double getRadius() {                 return radius;     }     /**     * set the radius     * @param newRadius                new radius of the circle           *throw exception if newRadius less than or equal to 0     */     public void setRadius(double newRadius) {                 if (newRadius <= 0.0)                   {                           throw new IllegalArgumentException("Radius must be greater than 0");                   }                 radius = newRadius;         }     /**     * Compute and return the area of the circle.     *     * @return    the area of the circle     */     public double area() {                 return Math.PI * radius * radius;     }     /**     * Stretches the size of the circle by multiplying     * the radius by the factor provided.     *     * @param    factor    factor to stretch by     *     */     public void stretchBy(double factor ) {         if (factor <= 0.0)                   {                           throw new IllegalArgumentException("Factor must be greater than 0");                   }                   radius = radius * factor;     }     /**     * Return a string representation of a circle.     *     * @return    a string representing this circle     */     public String toString() {       // implement body here                 return "Circle: "+radius;     } } // Circle```
Code:

```public class Exception {   public void Circle() throws ShapeException   {       System.out.println(         "Throwing ShapeException from Circle()");       throw new ShapeException("Examine method:Circle()");   }   public static void main(String[] args)   {       new Exception();   }   public Exception()   {       try       {         Circle();                              }         catch (ShapeException e)         {             System.out.println(e.getMessage());         }   } } class ShapeException extends Exception {     public ShapeException(String msg)   {       super(msg);   } }```
errors:
Exception.java:3: error: incompatible types
public void Circle() throws ShapeException
^
required: Throwable
found: ShapeException
Exception.java:7: error: incompatible types
throw new ShapeException("Examine method:Circle()");
^
required: Throwable
found: ShapeException
Exception.java:21: error: incompatible types
catch (ShapeException e)
^
required: Throwable
found: ShapeException
Exception.java:23: error: cannot find symbol
System.out.println(e.getMessage());
^
symbol: method getMessage()
location: variable e of type ShapeException
Exception.java:33: error: constructor Exception in class Exception cannot be applied to given types;
super(msg);
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
5 errors

----jGRASP wedge2: exit code for process is 1.
----jG
• 01-31-2013, 12:41 AM
Fubarable
Re: Need help creating my own exception for circle class
You have given one of your classes a name, Exception, that is the same as that of a critical core Java class and thus is shadowing the core class. Don't do that. Instead rename your Exception class to something that makes more sense like TestCircleException or somesuch.
• 01-31-2013, 01:02 AM
ndsmith20
Re: Need help creating my own exception for circle class
Thank you, I am going to try that now