Results 1 to 3 of 3
- 01-30-2013, 08:01 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
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; } } // Circleerrors: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); } }
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
-
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, 12:02 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Creating a personal exception class.
By Zoralink23 in forum New To JavaReplies: 1Last Post: 11-16-2012, 05:52 AM -
class Circle
By JojoDiaz in forum New To JavaReplies: 3Last Post: 10-12-2011, 06:38 AM -
Creating Exception class
By Bgreen7887 in forum New To JavaReplies: 1Last Post: 12-09-2010, 08:18 PM -
Help with creating, selecting and dragging a circle
By Jolee in forum Java 2DReplies: 3Last Post: 08-20-2009, 02:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks