In K&B pg.349 it is given that:-
It is illegal to use a try clause without either a catch clause or a finally clause.A try clause by itself will result in a compiler error.Any catch clauses must immediately follow the try block.Any finally clause must immediately follow the last catch clause (or it must immediately follow the try block if there is no catch)
It is legal to omit either the catch clause or the finally clause, but not both. but my program does not compile :-
class NewException extends Exception
{}
class
Test {
public static void main(
String[] args)
{
go(20);
}
public static void go(int i)
{
NewException ex = new NewException();
if(i<10)
try
{
throw ex;
}
finally
{}
}
}
C:\Test.java:15: unreported exception NewException; must be caught or declared to be thrown
throw ex;
^
1 error
Process completed.
I know that i am throwing a checked exception so that it should declared to be thrown or must be caught.
Is that mean P:-
It is legal to omit either the catch clause or the finally clause, but not both. is not applied to checked exceptions,you must have to catch it or declared to be thrown.
please clarify my doubts.....