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 is object of class in java?
What is string in java with example?
What are the five major types of reference sources?
Explain the differences between static and dynamic variables?
What is module in project?
What are the high-level thread states in java programming?
What is Session reduplication and how its done?
What are java methods?
How will you get the platform dependent values like line separator, path separator, etc., ?
What is method overloading in JAVA? Why is it not present in C ?
What are the types of relation?
Can we create object of inner class in java?
What is the exact difference in between Unicast and Multicast object?
How to create an immutable class?
Can subclass overriding method declare an exception if parent class method doesn't throw an exception?