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


Please Help Members By Posting Answers For Below Questions

What are alternatives to java serialization?

605


Tell me about different OOPS concepts.

597


How do you know if a value is nan?

573


What is the significance of listiterator?

571


Which sorting is used in arrays sort in java?

590






Can a main method be overloaded?

575


Why is java so important?

654


hr interview how many minutes asking question

1571


When will we use them?

601


Can we define a package statement after the import statement in java?

586


Why does abstract class have constructor?

562


what is anonymous class in java?

552


What is the main purpose of java?

521


What is a literal coding?

517


how to create daemon thread in java?

611