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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:05:53+00:00 2026-05-26T19:05:53+00:00

What is the best practice way of getting Exception Transparency in Java when using

  • 0

What is the best practice way of getting Exception Transparency in Java when using an anonymous inner class to run some code.

A frequent pattern that I have seen in real code is using some pseudo Runnable type interface to specify some context for some given code. The best example I can think of in the JDK is the java.security.PrivilegedExceptionAction.

try {
    boolean success = AccessController.doPrivileged(
        new PrivilegedExceptionAction<Boolean>() {
            @Override
            public Boolean run() throws Exception {
                // do something
                // read file
                FileInputStream fileInputStream = 
                    new FileInputStream(new File("someFile"));
                return true;
            }
        }
    );
} catch (PrivilegedActionException e) {
    if (e.getCause() instanceof FileNotFoundException) {
        // handle IO exception
    } else {
        // impossible no other checked exception
    }
}

Even though reading the code you can clearly see the inner code only throws a File not found, but we lost the benefits of checked exceptions as the caller is unaware of what exception is actually thrown. A Common bug would be to introduce code into the anonymous inner class that would thrown a new exception and the code would not force you to handle that exception.

What I want is something like what’s below, is this type of behaviour achievable without a language change?

public interface PrivilegedExceptionAction<T,V... extends Throwable>
{
    public T run() throws V;
}
  • 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-26T19:05:54+00:00Added an answer on May 26, 2026 at 7:05 pm

    I don’t see why not. The following code worked.

    interface RunIt < E extends Exception >
    {
        void run ( ) throws E ;
    }
    
    class App
    {
        public static void main ( String [ ] args )
        {
            RunIt < RuntimeException > r = new RunIt < RuntimeException > ( )
            {
                public void run ( )
                {
                    throw new RuntimeException ( ) ;
                }
            } ;
            r . run ( ) ;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a best-practice or common way in JavaScript to have class members as
Which of the following code is fastest/best practice for converting some object x? int
What's a jQuery like and/or best practices way of getting the original target of
I wondering what the best practice way (in C#) is to implement this xpath
is there any best practice way to replace a part of the default template.
what is the best practice or best way of passing form parameters from page
What is the common way (or best practice) to optionally use JNI? E.g. I
Is there a way in Cocoa that is currently considered best practice for creating
I'm trying to understand some fundamental best practices using Entity Framework. My EDM design
I'm having some trouble getting my cache to work the way I want. The

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.