This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

a Mock question  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 5390
1
Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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/)
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!