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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:37:36+00:00 2026-05-15T10:37:36+00:00

In the CLR (the runtime used by C#, VB.NET, etc.) there’s a way of

  • 0

In the CLR (the runtime used by C#, VB.NET, etc.) there’s a way of registering a callback to be called when an unhandled exception is thrown.

Is there anything similar in Java?

I’m guessing it would presumably be some API to which you’d pass an object that implements some interface with a single method. When an exception is thrown and there is no matching catch on the stack, the runtime would call the method on the registered object, and would be passed the exception object.

This would allow the programmer to save the stack trace. It would also allow them to call System.exit, to stop finally blocks executing only for unhandled exceptions.

Update 1.

To illustrate this, here is a sample in C#:

// register custom handler for unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += (sender, evt) =>
{
    Console.WriteLine("unhandled exception");
    Environment.FailFast(null);
};

try
{
    throw new NullReferenceException();
}
finally
{
    Console.WriteLine("finally is executing");
}

The point is that by putting in the call to Environment.FailFast(null) I can stop the finally block from executing.

Sure enough, in NET 3.5 and 4.0 running on Windows 7, I don’t see the “finally is executing” string in the output. But if I comment out the FailFast call, then I do see that string in the output.

Update 2.

Based on the answers so far, here’s my attempt to reproduce this in Java.

// register custom handler for unhandled exceptions
Thread.currentThread().setUncaughtExceptionHandler(

    new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(
                final Thread t, final Throwable e) {

            System.out.println("Uncaught exception");
            System.exit(0);
        }
    }
);

try
{
    throw new NullPointerException();
}
finally
{
    System.out.println("finally is executing");
}

When I run that in Java 6 (1.6.0_18) I see:

  • finally is executing
  • uncaught exception

In other words, the JRE executes finally blocks before executing the uncaught-exception handler.

For some background on why this is important, here’s a more complex example:

try
{
    try
    {
        throw new NullPointerException();
    }
    finally
    {
        System.out.println("finally is executing");
        throw new java.io.IOException();
    }
}
catch (java.io.IOException x)
{
    System.out.println("caught IOException");
}

System.out.println("program keeps running as if nothing had happened...");

So there’s a serious bug and I want my program to halt and log the stack trace. But before I can do this, there’s an intermediate finally block somewhere on the stack (in a real program it would be in a separate method) and it tries to access the file system. Something goes wrong. Then a little further up the stack suppose I have a catch for IOException because they’re not a big deal to me.

Needless to say, the output is:

  • finally is executing
  • caught IOException
  • program keeps running as if nothing had happened…

So now I have by accident created a situation in which serious bugs are hidden from me.

There are two solutions:

  • somehow ensure that finally blocks never throw, because they can’t locally tell if it is safe to. This is a shame because it is perfectly okay for them to throw on the normal execution path, i.e. when they are not running in response to a previous exception.
  • tell the runtime that I don’t want it to run finally blocks when there’s an uncaught exception.

The latter is certainly my preference if it’s available.

  • 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-15T10:37:37+00:00Added an answer on May 15, 2026 at 10:37 am

    See Thread.setUncaughtExceptionHandler().

    Update: as it turned out, the “unhandled / uncaught exception” seems to mean slightly different things in C# and Java. At first sight, the behaviour you describe seems to be (unfortunately for you) normal in Java:

    Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

    I.e. the uncaught exception handler is called when the exception has already been propagated out of user code, up into the Thread. This implies that it left the method containing the finally block, which has been duly executed.

    AFAIK finally blocks are always run in Java, so your Option 2 is not viable.

    Conclusion from the discussion below

    Option 1 – not throwing anything in finally blocks -, though limiting, seems to be the only real long term solution.

    For plainly the logging part, there is an Option 3 though:

    try
    {
        try
        {
            throw new NullPointerException();
        }
        catch (Exception x)
        {
            System.out.println("caught Exception" + x.getMessage());
            x.printStackTrace();
            throw x; // keep original behaviour
        }
        finally
        {
            System.out.println("finally is executing");
            throw new java.io.IOException();
        }
    }
    catch (java.io.IOException x)
    {
        System.out.println("caught IOException");
    }
    

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

Sidebar

Related Questions

Is there a way to host the .NET CLR runtime and register MethodImplOptions.InternalCall functions?
Does the .NET CLR runtime know how to optimize/inline simple property getters at runtime?
How can I get the current CLR Runtime version in a running .NET program
So when CLR runtime load a .NET assembly, it compiles it into machine native
Is there any where to get the CLR ID at runtime for the current
How are CLR (.NET) objects managed in SQL Server? The entry point to any
In Jeffrey Richter's CLR via C# (the .net 2.0 edtion page, 353) he says
How does JVM or for that matter CLR (Common language runtime) handles divided by
I am writing an MFC application that doesn't use .NET (CLR support is set
Windows 7 and Vista have the .NET 2.0/3.5 language runtime installed, so whatever you

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.