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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:22:33+00:00 2026-05-25T16:22:33+00:00

Yesterday I red this article about the new Exception Handling in Java 7. In

  • 0

Yesterday I red this article about the new Exception Handling in Java 7.

In the article they show an example (No 4) which is not working in Java 6. I just copied it.

public class ExampleExceptionRethrowInvalid {

public static void demoRethrow()throws IOException {
    try {

        // forcing an IOException here as an example,
        // normally some code could trigger this.
        throw new IOException("Error");
    }
    catch(Exception exception) {
        /*
         * Do some handling and then rethrow.
         */
        throw exception;
    }
}

public static void main( String[] args )
{
    try {
        demoRethrow();
    }
    catch(IOException exception) {
        System.err.println(exception.getMessage());
    }
}
}

Like in the article descriped it won’t compile, because of the type missmatch -throws IOException- and -throw exception-. In Java 7 it will. So my question is.

How do I proper implement this kind of rethrowing of an exception in Java 6? I don’t like the suggested implementation example no five. I know it is a matter of taste and problem you try to handle if unchecked exceptions or not. So what can I do to get the -throws IOException- and keep the stack trace? Should I only change the catch to IOException and risk not catching all?

I’m curious about your answers.

  • 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-25T16:22:34+00:00Added an answer on May 25, 2026 at 4:22 pm

    Simply catch IOException, like so:

    public static void demoRethrow()throws IOException {
        try {
            // forcing an IOException here as an example,
            // normally some code could trigger this.
            throw new IOException("Error");
        }
        catch(IOException exception) {
            /*
             * Do some handling and then rethrow.
             */
            throw exception;
        }
    }
    

    If the code inside the try block can throw a checked exception other than IOException, the compiler will flag this up as an error, so you’re not “risk[ing] not catching all”.

    If you’re also interested in unchecked exceptions, you could also catch and re-throw RuntimeException (you won’t need to declare it in the throws clause).

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

Sidebar

Related Questions

I asked a question just yesterday which caused this new issue. gdb within emacs
I asked this question yesterday about storing a plot within an object. I tried
Yesterday, in the presentation about the new Garageband for iPad 2, Apple demoed an
Yesterday I asked a question about parallel matrix multiplication in Java 7 using the
I already asked a question about this yesterday, but now I have another :)
When I try to run my Android project(which was working fine yesterday) I get
Yesterday, I asked this question and never really got an answer I was really
Yesterday, I discovered the Subversion (SVN) blame feature, and I was wondering, is this
Yesterday, I found myself writing code like this: SomeStruct getSomeStruct() { SomeStruct input; cin
Yesterday, I have a discussion with my colleagues about HTTP. It is asked why

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.