Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Calling a private method with an exception in a constructor

  1. #1
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Calling a private method with an exception in a constructor

    Hello I am having trouble calling a private method in my constructor. I have a private method called "dostuff()" that has an exception. I then called the method in my constructor "this.dostuff()" and I got and error saying "unreported exception java.lang.Exception; must be caught or declared to be thrown". Can someone give me insight on what to do, I'll really appreciate it.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Calling a private method with an exception in a constructor

    If doStuff() is declared to throw an exception then any method or constructor that calls it must either (A) do the call within a try/catch block or (B) that other method or constructor must also declare itself to throw the exception. Those are your options. See The Catch or Specify Requirement (The Java™ Tutorials > Essential Classes > Exceptions)

  3. #3
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Calling a private method with an exception in a constructor

    could you do a small example of those conditions?
    Last edited by Fubarable; 09-24-2012 at 12:42 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calling a private method with an exception in a constructor

    Please click on the link that pbrockway provided as there you'll find explanation and examples.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Calling a private method with an exception in a constructor

    Quote Originally Posted by Fubarable View Post
    Please click on the link that pbrockway provided as there you'll find explanation and examples.
    ... and if there is something *specific* about what you find there that you can't understand, ask about that.

    No-one is pretending that technical tutorials are easy to read, nor are we trying to ignore your question. But the fact is that Oracle's Tutorial has had a lot of close editorial attention paid to it, over a long time. Anything I might write off the top of my head would be, at best, an "explanation-lite".
    Fubarable likes this.

Similar Threads

  1. Replies: 4
    Last Post: 04-22-2012, 10:22 PM
  2. Calling method from a different class into a constructor
    By NewToJava_99 in forum New To Java
    Replies: 1
    Last Post: 11-28-2011, 12:00 AM
  3. Private constructor and super()
    By Basit56 in forum Advanced Java
    Replies: 9
    Last Post: 08-21-2009, 07:19 AM
  4. [SOLVED] Accessing private constructor
    By piyu.sha in forum New To Java
    Replies: 2
    Last Post: 10-06-2008, 06:45 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •