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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:37:20+00:00 2026-05-16T03:37:20+00:00

I am trying to add a custom throws clause to a method definied by

  • 0

I am trying to add a custom throws clause to a method definied by an interface. This is not possible. How could I bypass it? Here is some code:

private void sendRequestToService(final ModuleRequest pushRequest) 
{

    ServiceConnection serviceConnection = new ServiceConnection()
    {

        public void onServiceConnected(ComponentName name, IBinder service) 
        {

            try
            {

                //some lines..

            } catch (RemoteException e)
            {
                throw new RuntimeException(new UnavailableDestException()) ;
            }
        }


    };

}

Any idea how I could throw my custom exception?

  • 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-16T03:37:21+00:00Added an answer on May 16, 2026 at 3:37 am

    There are two types of exceptions, checked and unchecked. Any Throwable is either one or the other.

    An example of a checked exception is IOException; probably the most (in)famous unchecked exception is NullPointerException.

    Any checked exceptions that a method may throw must be declared in its throws clause. When you @Override a method (either implementing an interface method or overriding an inherited method from a superclass), certain requirements must be met, and one of them is that the throws clause must not cause a conflict. Simplistically speaking, subclasses/implementations can throw LESS, not MORE checked exceptions.

    An unchecked exception is defined as RuntimeException and its subclasses, and Error and its subclasses. They do not have to be declared in a method’s throws clause.

    So in this particular case, if you want to throw a CustomException in an implementation of an interface method that does not list it in its throws clause, you can make CustomException extends RuntimeException, making it unchecked. (It can also extends any subclass of RuntimeException, e.g. IllegalArgumentException or IndexOutOfBoundsException may be more appropriate in some cases).

    This will allow you to compile the code as you desire, but note that the choice between choosing checked vs unchecked exception should not be taken too lightly. This is a contentious issue for many, and there are many factors to consider other than just getting the code to compile the way you want it. You may want to consider a redesign of the interface rather than having implementors throwing various undocumented unchecked exceptions not specified by the interface contract.

    References

    • JLS 11.2 Compile-Time Checking of Exceptions
    • JLS 8.4.6 Method Throws

      A method that overrides or hides another method, including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

    Related questions

    • In Java, when should I create a checked exception, and when should it be a runtime exception?
    • When to choose checked and unchecked exceptions
    • The case against checked exceptions

    See also

    • Effective Java 2nd Edition
      • Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
      • Item 59: Avoid unnecessary use of checked exceptions
      • Item 60: Favor the use of standard exceptions
      • Item 61: Throw exceptions appropriate to the abstraction
      • Item 62: Document all exceptions thrown by each method

    Workaround “solution”

    If a redesign is impossible, then wrapping your CustomException in a RuntimeException (or its subclass) will “work”. That is, instead of:

    // ideal solution, not possible without redesign
    
    @Override public static void someMethod() throws CustomException {
        throw new CustomException();
    }
    
    //...
    try {
        someMethod();
    } catch (CustomException e) {
        handleCustomException(e);
    }
    

    You can, should you insist, do the following:

    // workaround if redesign is not possible
    // NOT RECOMMENDED!
    
    @Override public static void someMethod() {
        throw new RuntimeException(new CustomException());
    }
    
    //...
    try {
        someMethod();
    } catch (RuntimeException e) { // not catch(CustomException e)
    
        if (e.getCause() instanceof CustomException) {
            handleCustomException((CustomException) e.getCause());
        } else {
            throw e; // preserves previous behavior
        }
    
    }
    

    It needs to be reiterated that this is NOT a recommendable technique in general. You should fix the problem at the design level if at all possible, but barring that, this is indeed a possible workaround.

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

Sidebar

Related Questions

I'm trying to add a custom catalog index based on the recommendation here: http://collective-docs.readthedocs.org/en/latest/searching_and_indexing/indexing.html#custom-index-methods
I am trying to add a custom UIBarButtonItem to my navigationItem. This button will
I am trying to add a custom button in the new Xcode 4 Interface
I am trying to add custom icons to a Shared Add-in for Microsoft Word.
I'm creating a custom WP theme and trying to add custom menus to the
I'm trying to add a custom class to the jQuery Mobile theme. The CSS
I am trying to add a custom log handler to my java application. I
I am trying to add a custom field to the user form in the
I am trying to add a custom property to a base form that can
I am trying to add a custom view control to a custom UITableViewCell which

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.