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 8492133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:37:51+00:00 2026-06-10T22:37:51+00:00

So I have a try/finally block. I need to execute a number of methods

  • 0

So I have a try/finally block. I need to execute a number of methods in the finally block. However, each one of those methods can throw an exception. Is there a way to ensure that all these methods are called (or attempted) without nested finally blocks?

This is what I do right now, which is pretty ugly :

protected void verifyTable() throws IOException {
    Configuration configuration = HBaseConfiguration.create();
    HTable hTable = null;                                               

    try {
        hTable = new HTable(configuration, segmentMatchTableName);      

        //...
        //various business logic here
        //...

    } finally {                         
        try {
            try {
                if(hTable!=null) {
                    hTable.close(); //This can throw an IOException
                }               
            } finally {
                try {
                    generalTableHelper.deleteTable(configuration, segmentMatchTableName); //This can throw an IOException
                } finally {
                    try {
                        generalTableHelper.deleteTable(configuration, wordMatchTableName); //This can throw an IOException
                    } finally {
                        generalTableHelper.deleteTable(configuration, haplotypeTableName); //This can throw an IOException      
                    }
                }
            }                               
        } finally {
            HConnectionManager.deleteConnection(configuration, true); //This can throw an IOException   
        }
    }               
}

Is there a more-elegant way to do this?

  • 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-06-10T22:37:53+00:00Added an answer on June 10, 2026 at 10:37 pm

    The standard (working) way to right resource management in Java (the principle applies to other languages as well) is:

    Resource resource = acquire(resource);
    try {
        use(resource);
    } finally {
        resource.release();
    }
    

    Or using the shortcut (with an extra bit of cleverness) in the current version of Java SE:

    try (Resource resource = acquire(resource)) {
        use(resource);
    }
    

    (As Joe K points out, you may need to wrap the resource to make it confirm to the specific interface that the Java language depends upon.)

    Two resources, and you just apply the idiom twice:

    Resource resource = acquire(resource);
    try {
        SubResource sub = resource.acquire();
        try {
            use(sub);
        } finally {
            sub.release();
        }
    } finally {
        resource.release();
    }
    

    And in Java SE 7:

    try (
        Resource resource = acquire(resource);
        SubResource sub = resource.acquire()
    ) {
        use(resource, sub);
    }
    

    The really great advantage of the new language feature is that resource handling was more often than not broken when written out.

    You might have more complicated exception handling. For instance, you don’t want to throw low-level exceptions such as IOException through to the application proper – you probably want to wrap in some subtype of RuntimeException. This can, with Java’s typicaly verboseness, be factored out using the Execute Around idiom (see this excellent question). From Java SE 8, there will also be shorter syntax with randomly different semantics.

    with(new ResourceSubAction() { public void use(Resource resource, SubResource sub) {
        ... use resource, sub ...
    }});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a try catch and finally block like this. Client client = new
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
Possible Duplicates: Conditions when finally does not execute in a .net try..finally block In
I have some critical logic in a finally block (with an empty try block),
Say I have some code like this: try: try: raise Exception(in the try) finally:
In my services all exposed methods have: try { // the method core is
Is it considered bad practice to have multiple try's in one method and structure
Is there a way to dispose method variables without using try-finally block in C#
I need to understand java exception handling priority. I have a class Controller, having
There are 3 permutations of a try...catch...finally block in java. try...catch try...catch...finally try...finally Once

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.