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

The Archive Base Latest Questions

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

A RuntimeException is thrown in try block without being caught, while the finally clause

  • 0

A RuntimeException is thrown in try block without being caught, while the finally clause invokes System.exit().

public static void main(String[] args) {
    try {
        Integer.valueOf("NotANumber");
    } finally {
        System.out.println("finally");
        System.exit(0);
    }
}

The output is

finally

If System.exit(0) is removed from finally, then the output is

finally
Exception in thread "main" java.lang.NumberFormatException: For input string: "NotANumber"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.valueOf(Integer.java:554)
    at exception.MyExcepTest.main(MyExcepTest.java:20)

Where “finally” may appears before, after or in between the meesage of NumberFormatException.

Can anybody explain it?

  • 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-27T00:28:19+00:00Added an answer on May 27, 2026 at 12:28 am

    The finally block will definitely be executed before the main method exits, and the stacktrace is printed by the JVM after that.

    Maybe the stacktrace gets printed to System.err, and the two streams get mixed up in your console output in unpredictable ways (since they are produced basically simultaneously).

    What happens when you print “finally” to System.err as well?

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

Sidebar

Related Questions

I want to ask why we don't have to add try-catch block to a
Here's my code: class FinallyDemo { static void myMethod(int n) throws Exception{ try {
Is there any possibility of exceptions to occur other than RuntimeException in Java? Thanks.
class ExtHotelApiService extends HotelApiService { static scope = singleton static transactional = true def
Let's say I have the following Java interface that I may not modify: public
I am trying to add a custom throws clause to a method definied by
I'm trying to use the @Test(expected = RuntimeException.class) annotation in order to test for
Ran into a problem. I have a spawned thread that must call its finally
Here is the code that generates .xls file using JXL: public void generateXls(String fileName,
I'm trying to fix an issue, in my application I have this code try

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.