In: Computer Science
Revise this code to handle exceptions and provide correct total number of valid objects.
(1)
/** * class: CircleWithException * description: This program creates CircleWithException specification by using an instance variable/data/attribute named * radius. * Then, also we will create a static variable named numberOfObjects to count number of object when you create in a tester program. * In this program, you will learn how you declare an exception and define the exception. * This is a part of checked exception process. * Regarding the checked exception, besides run time error exceptions, all other exceptions declared must be checked when * a caller calls the method with a checked exception. */ public class CircleWithException { private double radius; private static int numberOfObject = 0; //1 public CircleWithException(){ this(1.0); } //2 public CircleWithException(double rad){ //this.radius = rad; setRadius(rad); numberOfObject++; } public double getRadius() { return radius; } /** * method: setRadius * @param radius * @throws IllegalArgumentException * * description: setRadius method throws an exception if the radius is negative. * this case, this method will throw an IllegalArgumentException. * throws is the keyword to indicate that this method throw this type of exception. * the caller must MUST MUST handle the exception using a try-catch block or we put "throws any exception from * the main method. * We can throw either checked or unchecked exceptions. * * The keyword throws will allow the compiler to help you write a code to handle this type of error. * BUT it does not prevent the abnormal termination of the problem. With the help of the throws keyword, * you can provide the information to the caller of the method about the type of the exceptions the method might throw. */ public void setRadius(double radius) throws IllegalArgumentException { //declare the IllegalArgumentException if(radius >= 0) this.radius = radius; else throw new IllegalArgumentException("Radius cannot be a negative number. "); } //eureka public static int getNumberOfObject() { return numberOfObject; } //if you see static then it is a class method. a class method is also called a static method. //static method is called by the classname when it is called outside of the class which defined that method. //inside of CircleWithException, you call getNumberOfObject() //outside of CircleWithException, you call getNumberOfObject by using this format: CircleWithException.getNumberOfObject() //if you do not include static, then it is an instance method. it is also called non-static method. //with an example of area method, if you create an object name c1, then you call area() by the object named c1. //c1.area() public double area() { return radius * radius * Math.PI; } } (2)
public class TesterCircle { public static void main(String[] args) { try { CircleWithException c1 = new CircleWithException(0); CircleWithException c3 = new CircleWithException(-5); CircleWithException c2 = new CircleWithException(5); } catch(IllegalArgumentException e){ System.out.println(e); } System.out.println("Number of Objects created: " + CircleWithException.getNumberOfObject());// } }
If you clearly analyze and dry-run the code, what happens is when we construct first object, c1 with parameter as 0, since this parameter satisfies the condition for drawing a circle on a plane so increment the count to 1(Since numberOfObjects is a static member, so it will be available for all objects).
Now, when we try to construct the second object c2 with value as -5, is violates our condition and our setRadius method throws the Exception as "Radius cannot be a negative number."
As per the rule of try-catch, when exception is thrown, the program just terminates and passes control to catch block, where you can use the exception to notify the user about the problem. And just after its execution, the control goes out of catch and continues the normal execution.
Hence , Total no. of objects created will be 1 only, because after trying to constructing c2, the excution flowed to catch block and then the System.out.println() statement.
So on the Console you will see output as -->
java.lang.IllegalArgumentException: Radius cannot be a negative number. Number of Objects created: 1
Exception due to negative argument and received by catch block where it is printed and then normal print statement executed to check no. of objects created.