• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exciting important exception question -- please help

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
10. What is the result when you compile the and run the following code?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A) Compilation error
B) Runtime error
C) Compile successfully, nothing is printed.
D) inside demoMethod. followed by caught: java.lang.IllegalAccessExcption: demo

// the answer is A, but I chose D because I thought that, if an exception had been thrown, if there was no catch block in the current block, then the calling method could take care of the exception.
IS IT BECAUSE THERE WAS NO TRY-CATCH OR THROWS CLAUSE ?

 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the expected behaviour.
Take a look at JLS Section 14.17 The throw Statement


.....at least one of the following three conditions must be true, or a compile-time error occurs:

The exception is not a checked exception (�11.2)-specifically, one of the following situations is true:


  • The type of the Expression is the class RuntimeException or a subclass of RuntimeException.
  • The type of the Expression is the class Error or a subclass of Error.

  • The throw statement is contained in the try block of a try statement (�14.19) and the type of the Expression is assignable (�5.2) to the type of the parameter of at least one catch clause of the try statement. (In this case we say the thrown value is caught by the try statement.)
    The throw statement is contained in a method or constructor declaration and the type of the Expression is assignable (�5.2) to at least one type listed in the throws clause (�8.4.4, �8.8.4) of the declaration.


Clearly your code violates the last rule and hence the compiler error.
Hope that helps,
Ajith
[This message has been edited by Ajith Kallambella (edited December 14, 2000).]
 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot ajith, that was brilliant!
I do understand that if there is a potential of a chekced exception being thrown, then either there must be a try-catch block or it must be declared in throws otherwise a compiler error takes place.
The issue I am concerned about is that I had read somwhere, that if the exception cant be handled in the current block, then it is passed to the calling method, and in this case the calling method does take care of business. What is the significance of this and is that only valid (stops the comp. error, if there had been a throws?).
I hope I made my point clear.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic