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 a marker interface?
Is {a, n, d} a palindrome? If you are given a random string, is it a palindrome or not?
What is the default value of float and double datatype in java?
What are the 7 types of characters?
Is an object null?
What restrictions are placed on method overriding?
What is void keyword?
Can a class have an interface?
What is class forname used for?
What is a subsequence of a string?
What is unicode used for?
Is assembly language a low level language?
What is the largest data type in java?
Why synchronization is important?
Where are local variables stored?