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

  • Home
  • SEARCH
  • 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 3611014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:49:28+00:00 2026-05-18T21:49:28+00:00

public void foo() { begin(); try { … commit(); } catch (Exception e) {

  • 0
public void foo() {
 begin();
 try {
  ...
  commit();
 } catch (Exception e) {
  rollback();
  throw e;
 }
}

In the sample above, there is an error because foo has no throws Exception. Adding that wouldn’t make do the method’s usability a lot of good either.

What’s the best way to do this? How do you do something if an error occurs without really “handling” the error?

  • 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-18T21:49:29+00:00Added an answer on May 18, 2026 at 9:49 pm

    At least two approaches come to mind, which are usually going to be combined depending on what you want foo to do:

    1. Catch and rethrow only the relevant exceptions

    There are only so many exceptions the code in your main flow can throw (probably mostly SqlExceptions). So only catch and rethrow those, and declare that you’re doing so. More to the point, rethrow only the ones you’re not actually handling (in your simplified sample code, you’re not handling any, but your real life code is probably more subtle).

    Mind you, some of the exceptions may be runtime exceptions, and so you may want to combine this with the below.

    2. Don’t catch the exception at all

    Like this:

    // Signature changes to include any exceptions that really can be thrown
    public void foo() throws XYZException, ABCException {
     // A flag indicating that the commit succeeded
     boolean done = false;
    
     begin();
     try {
      // Don't have any `return` statements in here (or if you do,
      // set `done` to `true` first)
    
      ...
      commit();
      done = true; // Commit didn't throw an exception, we're done
     } finally {
      // finally clause always happens regardless
      if (!done) {
        // We must be processing an exception; rollback
        try {
          rollback();
        } catch (Exception e) {
          // quash it (e.g., leave this block empty), we don't want
          // to mask the real exception by throwing a different one
        }
      }
     }
    }
    

    Naturally your signature needs to include any exceptions that may be thrown in the main flow, but that’s what you’re trying to do, if I’m understanding you correctly.

    Again, you may well combine these two approaches, because you may want to handle some exceptions and not others.

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

Sidebar

Related Questions

Given a declaration like this: class A { public: void Foo() const; }; What
Just a general question about what the best practice is: public void Foo() {
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
If I have the following code: public class Foo { public void Bar() {
public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int
[TestMethod] public void Memory() { var wr = new WeakReference(aaabbb); Assert.IsTrue(wr.IsAlive); GC.Collect(); GC.Collect(); GC.Collect();
For example: public void doSomething() { final double MIN_INTEREST = 0.0; // ... }
My Program overrides public void paint(Graphics g, int x, int y); in order to
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
E,g class Test { public: void setVal(const std::string& str) { this.isVal = str; //This

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.