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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:23:00+00:00 2026-05-27T06:23:00+00:00

Inside my class I have a private variable that my methods use which has

  • 0

Inside my class I have a private variable that my methods use which has an exception that has to be caught.

How can I force the callee to handle the exception and not have me do it in my code?

public class SomeLibImpl extends SomeLib {

   private Mongo _mongo = new Mongo();

}

Where Mongo throws a UnknownHostException that I have to handle.

If this is something I shouldn’t be doing and there is a better way please tell me, but I also want to know how to force the callee of the class to handle the exception.

I thought I would do this:

public class SomeLibImpl extends SomeLib throws UnknownHostException {
  // ..
}

But that doesn’t compile.

  • 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-27T06:23:00+00:00Added an answer on May 27, 2026 at 6:23 am

    Either you should initialize mongo in your constructor, and put the throws clause on the constructor, or you should pass it in to the constructor (inject it).

    e.g.

    public class Foo extends Bar {
      private Mongo mongo;
      public Foo() throws SomeException { 
        mongo = new Mongo();
      }
      // stuff goes here
    }
    

    Or you can inject the object into yours…

    public class Foo extends Bar {
      private Mongo mongo;
      public Foo(Mongo mongo) { 
        this.mongo = mongo;
      }
      // stuff goes here
    }
    

    The second style of object construction (dependency injection) is better than initializing in the class, because it allows you to substitute other objects if you want to perform tests on the object but don’t want to have to depend on a database to do so, then you can inject a stub (or mock) version of the object and test your object in isolation.

    Also, you can’t force users of your class to handle exceptions that your code throws. Even if you throw a checked exception, they can ignore it or simply declare that their method throws the exception too, which passes the buck to whatever code calls them.

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

Sidebar

Related Questions

Assume the following: we have class B, which is a private class nested inside
I have learned that in a class you can declare variables and methods. They
So I have a class which holds a struct inside its private variables and
Normally when I have a private field inside a class or a struct, I
I have this code inside a class that is used by an application and
I have a function inside a class that returns a reference to a member
I have a property inside a class that is getting changed by something. The
I have a couple of functions inside a class that essentially do the same
I have a class called SqliteImageAccess that is going to have various methods that
I've read that you cannot declare static variables/methods inside a generic class and I

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.