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
What are alternatives to java serialization?
Tell me about different OOPS concepts.
How do you know if a value is nan?
What is the significance of listiterator?
Which sorting is used in arrays sort in java?
Can a main method be overloaded?
Why is java so important?
hr interview how many minutes asking question
When will we use them?
Can we define a package statement after the import statement in java?
Why does abstract class have constructor?
what is anonymous class in java?
What is the main purpose of java?
What is a literal coding?
how to create daemon thread in java?