This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Exceptions

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all, a few questions about exception handling in java.
First off: I understand that the finally{} block is meant to execute regardless of success or failure in the try{} block. So if I have the following:

This is by the book but strangely enough if I have the following:

Are there only certain circumstances where the finally block is essential to run code after an exception has been caught in a catch block? Or is it a matter of surety that code will run regardless of success or failure?
Question 2:
If I have a method:

I get a compile error if I try to use the method without an exception handler.
BUT...
if I have a method:

Java lets me compile without an exception handler when calling the method. Wierd, has anyone else run into this?
Tetsuo!
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have some basic misunderstandings of the mechanisms. In your example:
try{
//blah
}catch(Exception e){
//blah
}
System.out.println("This always occurs");
// the above runs *anyway*, without the finally{}
The output statement will execute as part of the normal flow within the method. When you catch an exception as shown here, flow continues normally after the try/catch/finally block.
If you did not catch the exception, or if you threw another exception in your handler, the output statement would not be called in this example, but would be called if it was in a finally block.
hth,
bear
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for #2:

NumberFormatException is a subclass of java.lang.IllegalArgumentException, which in turn, is a type of java.lang.RuntimeException.

Reading the API for this exception type, we find:
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Are there only certain circumstances where the finally block is essential to run code after an exception has been caught in a catch block? Or is it a matter of surety that code will run regardless of success or failure?


here is an example

in the above, since there is a return statement with catch. Once there is an exception, cleanup 2 is not executed. But cleanup 1 will be executed if there is an exception or not.
kawaii
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic