Results 1 to 3 of 3
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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:

    Java 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
    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help creating my own exception for circle class

    Thank you, I am going to try that now

Similar Threads

  1. Creating a personal exception class.
    By Zoralink23 in forum New To Java
    Replies: 1
    Last Post: 11-16-2012, 06:52 AM
  2. class Circle
    By JojoDiaz in forum New To Java
    Replies: 3
    Last Post: 10-12-2011, 07:38 AM
  3. Creating Exception class
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 09:18 PM
  4. Replies: 3
    Last Post: 08-20-2009, 03:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •