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
Differentiate between overriding and overloading cases?
What is file in java?
What is protected in java?
What is callablestatement? How you can call stored procedure to pass in parameter?
Explain jdk, jre and jvm?
How many bits is a 64 bit byte?
Define how objects are stored in java?
What is hash table in java?
What is method in research paper?
Explain exception chaining in java?
What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?
What is bom encoding?
Write a program to show whether a graph is a tree or not using adjacency matrix.
What is module in oop?
What does singleton class mean?