Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:14:27+00:00 2026-05-25T15:14:27+00:00

As I understand, the following 2 examples should do the same thing. Why is

  • 0

As I understand, the following 2 examples should do the same thing. Why is the 1st considered better?

1:

try {
  riskyMethod();
}
catch(Exception e) {
  //handle exception
}
finally {
  cleanUp();
}

2:

try {
  riskyMethod();
}
catch(Exception e) {
  //handle exception
}
cleanUp();

EDIT: The example is in Java, but I’m wondering about the concept of a finally block in general, as used in any language

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:14:28+00:00Added an answer on May 25, 2026 at 3:14 pm

    Well, for one thing if the “handle exception” part throws an exception itself, cleanup won’t occur.

    More importantly though, you should almost never be catching all exceptions. You should catch very specific exceptions you can handle, and let other exceptions bubble up. At that point, you must use a finally block if you want the cleanup to still occur.

    It’s not clear what language you’re using, but if it’s Java then there’s already a difference, as non-Exception exceptions (other subclasses of Throwable) will end up cleaning up in your first version but not in your second – but you shouldn’t even catch Exception generally.

    Personally I find I write more try/finally blocks than try/catch or try/catch/finally blocks. I find it pretty rare that I can really deal with an exception… although sometimes I catch one exception just to convert it to one more appropriate for the abstraction level I’m working on, and then rethrow.

    EDIT: As noted in dj aqeel’s answer, finally statements are also executed if the block completes without an exception, e.g. through a return statement. The very fact that I’d forgotten that is a good reason to favour finally: it promotes consistency. It’s one consistent place to perform clean-up, regardless of how the block is exited.

    Also note that in C#, you’d idiomatically use a using statement for disposable resources. And Java 7 has the try-with-resources statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone help me to understand following line of code: sol< ?=f((1<< n)-1,i,0)+abs(P[i])*price; I
I understand the following elements : there is some sort of crawling (configured in
Can't understand why the following takes place: String date = 06-04-2007 07:05; SimpleDateFormat fmt
I don't understand why the following code generates a warning. interface Generic<T> { }
I'm lead to understand that the following grants all proveleges to all databases that
So, I understand that the following doesn't work, but why doesn't it work? interface
I dont understand what would be the problem with the following code. It needs
if wrote the following code and do not understand why the trace returns false:
I really don't understand regular expressions and was wondering what the following regular expressions
I have come across the following jQuery code but could not understand it. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.