Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try/catch/finally

 
Venkatesh Gowri
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I have a doubt regarding Try/catch and Finally .
Now will "Finally" execture even if there is no catch block after Try.
That is if The runtime System throws an exception Due to the absence of the Catch Block will the Finally Block Still be Executed.?

Now in the above example i have commented the catch block but this is the output i get an Arithmetic Exception but the finally block never gets exectued.
can u please Help me
thanks
 
manasa teja
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finally gets executed always, if it is present, except special cases like, System.exit
Even in ur example also, the output is
Hello
Exception in thread "main" java.lang.ArithmeticException: / by zero
at divbyzero.main(divbyzero.java:9)
It directly goes to Finally block as the exception is not caught and exceutes finally and gives exception.
HTH
 
Tauqueer Ali
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code doesn't even compile
gives the following error message while compiling
cannot compute value of expression: class Error const *.
 
suresh kamsa
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiled and raised Arithemetic exception after printing Hello on the screen.
Sample output.
--------------
Hello
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.main(test.java:9)
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get:
divbyzero.java:9: Arithmetic exception.
int y=10/0;
at compile time.
Do different compilers act differently when they see constant integer division by 0?
 
suresh kamsa
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiled and raised Arithemetic exception after printing Hello on the screen.
Sample output.
--------------
Hello
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.main(test.java:9)
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic