• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

a Mock question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody can tell me the execute process of follow snippet:
1. class Unchecked {
2. public static void main(String args[]) {
3. try {
4. method();
5. } catch(Exception e) { }
6. }
7. static void method() {
8. try {
9. wrench();
10. System.out.println("a");
11. } catch(ArithmeticException e) {
12. System.out.println("b");
13. } finally {
14. System.out.println("c");
15. }
16. System.out.println("d");
17. }
18. static void wrench() {
19. throw new NullPointerException();
20. }
21. }
thanks~
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Chan:
anybody can tell me the execute process of follow snippet:
1. class Unchecked {
2. public static void main(String args[]) {
3. try {
4. method();
5. } catch(Exception e) { }
6. }
7. static void method() {
8. try {
9. wrench();
10. System.out.println("a");
11. } catch(ArithmeticException e) {
12. System.out.println("b");
13. } finally {
14. System.out.println("c");
15. }
16. System.out.println("d");
17. }
18. static void wrench() {
19. throw new NullPointerException();
20. }
21. }
thanks~


finally gets executed and c gets printed out
nullptr exception is not handled in method() so we except that exception to be thrown out
But its caught in the caller main method ... so there is no
exeception output...only finally printouts
enjoy
Ragu
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,
i just ran the code and i found out that ragu is correct. at first i disagreed (in my mind) because i thought the code wouldn't compile at all. to my surprise, it compiles! hmm, i threw other exceptions and i get compilation error "exception has to be declared to be thrown". is that only NullPointerException does not require a declaration before being thrown? is there any other exceptions that doesn't need a declaration?
(pls don't put me in the drawing for the book, my test is scheduled to be next tuesday)
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chin,
there are 2 types of exceptions.
1. Unchecked Exception : Error and its subclasses and Runtime exceptions are unchecked one.
2. Checked Exception : all remaining classes are checked one.
The programmer has to deal with checked exception by means of try-catch block or throws clause. But for unchecked one , they can be handled by default exception handler. That's why---


i threw other exceptions and i get compilation error "exception has to be declared to be thrown".


try to modify fn like this...
static void wrench() {
throw new RunTimeException();
}
}
it'll compile ...being an unchecked exception.
but for checked one, u have to have either try-catch or throws clause.
Hope this helps
Rashmi
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 'c' and 'd' will be printed out.
CMIW
------------------
Regards
Ravish
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only c will be printed out because the exception thrown by the method wrench() is not handled in method(), so the part after finally will not be executed.

------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)
 
reply
    Bookmark Topic Watch Topic
  • New Topic