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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:00:31+00:00 2026-05-17T18:00:31+00:00

I run this code: public class User { public static void main(String args[]) {

  • 0

I run this code:

public class User {

    public static void main(String args[]) {
        int array[] = new int[10];
        int i = 1;
        try {
            System.out.println("try: " + i++);
            System.out.println(array[10]);
            System.out.println("try");
        } catch (Exception e) {
            System.out.println("catch: " + i++);
            System.out.println(array[10]);
            System.out.println("catch");
        } finally {
            System.out.println("finally: " + i++);
            Object o = null;
            o.hashCode();
            System.out.println("finally");
        }

    }
}

Result:
try: 1
catch: 2
finally: 3
Exception in thread
“main” java.lang.NullPointerException
at user.main(User.java:17)

in block catch – ArrayIndexOutOfBoundsException, but we loss this Exception, why?

  • 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-17T18:00:31+00:00Added an answer on May 17, 2026 at 6:00 pm

    From the JLS

    You can read about this in the JLS, Blocks and Statements, section “14.19.2 Execution of try-catch-finally”. And I quote,

    If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:

    • If the finally block completes normally, then the try statement completes abruptly for reason R.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded). The example…

    Therefore, the following (which is really condensed from the questioner’s code) completes with an NPE, not the ExceptionTest thrown.

    class Phinally
    {
      static class ExceptionTest extends Exception
      { public ExceptionTest(String message) { super(message); }  }
    
      public static void main(String[] args) throws ExceptionTest
      {
        try {
          System.out.println("Foo.");
          throw new ExceptionTest("throw from try"); 
        } finally {
          throw new NullPointerException("throw from finally");
        }    
      }
    }
    

    A Sidebar about try with resources/ARM blocks

    Difficulties reasoning about this in some common cases specifically with managing resources, and requiring nested try/catch/finally blocks, and nested inside finally blocks, are part of the reason for the “try with resource” feature in project COIN (to be integrated into Java “fairly soon”), about which you can read more about here.

    This is one of many good reasons to invest the time in running a static analyzer like PMD, which finds and complains about this type of confusion — though it might not catch the case in your code, I’m not sure.

    Static Checking

    Follow up on comment from @stacktrace: I ran the relevant code through PMD and FindBugs, trying both of the following:

    finally { throw NullPointerException("Foo"); }
    

    and

    finally { Object o = null; System.out.println(o.toString()); }
    

    For the former, PMD noticed and complained about an exception being thrown from a finally clause. FindBugs doesn’t complain at all. For the latter, PMD complained about several things but nothing related (“LocalVariableCouldBeFinal”, “StringToString”, and “UselessOperationOnImmutable”). However, FindBugs noticed and complained about a null dereference. Moral of the story? Run both PMD and FindBugs!

    Related

    Related on SO: Swallowing exception thrown in catch/finally. Can I avoid such cumbersome try/catch/finally…

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

Sidebar

Related Questions

when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
When I run this code the selected item is not visible. I've already tried
I have a question concerning this code which I want to run on QNX:
I receive this Run-Time Check Failure upon the return in the following code. I
I run into this quite often where a new page is supposedly tested and
This is my code I have developed. This is the main program which holds
This is my situation: i need the code to run in a loop, over
Oracle is giving me an error (ORA-00907: missing right parenthesis) when I run this
With SQL Server 2005 using SSMS, when I run this: SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel
I'm working with a MySQL query that writes into an outfile. I run 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.