What is the catch or declare rule for method declarations

If a checked exception may be thrown within the body of a method, the method musteither catch the exception or declare it in its throws clause.

Showing Answers 1 - 3 of 3 Answers

vegetto

  • Jun 15th, 2008
 

this rule is actually for handling the exceptions:

that is there are two ways by ehich u can handle the exception :

1:either you use the try catch statement and enclose the appropriate code in the try catch block,this is called as the catch or the HANDLE rule

2:you can also use the throw and throws statement instead of using the try catch block i.e you should specify the exceptions that your method  is capable of throwing(only checked)

eg void MyMethod() throws IOEception,any other exception just separate them with commas................
{
   /*your code and throw statement at appropriate step that throws the exception
    and don't use the try catch statement in this case
 EG throw new MyException(arguments if any);
  this instantiates MyException class*/
}
 this is called as DECLARE RULE

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions