what is difference between throw and throws in exception?
Answer Posted / amol nakhwa
/*
Save this file as "TestCircle.java"
*/
class NegativeRadiusNotAllowedException extends Exception
{
public String getMessage()
{
return "Radius should be positive number only!";
}
}
class Circle
{
public void calculateArea(int rad) throws
NegativeRadiusNotAllowedException
{
if (rad < 0)
{
throw new NegativeRadiusNotAllowedException();
}
double ar = 3.1415*rad*rad;
System.out.println("Circle Radius = "+ rad + " and Area =
"+ar);
}
}
public class TestCircle
{
public static void main (String args[])
{
Circle c = new Circle();
try
{
//int no1 = Integer.parseInt(args[0]);
//int no2 = Integer.parseInt(args[1]);
c.calculateArea(9);
c.calculateArea(-9);
}
catch (NegativeRadiusNotAllowedException e)
{
System.out.println(e.getMessage());
}
catch (NumberFormatException e1)
{
System.out.println(e1.getMessage() +" is not a valid
integer");
}
catch (Exception e2)
{
System.out.println(e2.getMessage());
}
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Can we have two methods in a class with the same name?
How does hashset works in java?
Explain the use of volatile field modifier?
What does int argc char * argv [] mean?
How do you use compareto?
What is oop principle in java?
How do you convert boolean to boolean?
What is sorting algorithm in java?
What is main string [] args?
Why do we use regex?
What are the different http methods?
what is the significance of listiterator in java?
What do you mean by buffering?
Are arrays immutable in java?
How to compare strings in java?