Results 1 to 12 of 12
  1. #1
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default Exceptions rules for Construtors !!

    Hey all,

    Can construtor throws Exceptions?? Is there anylimitation of doing that?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by chathura87 View Post
    Hey all,

    Can construtor throws Exceptions?? Is there anylimitation of doing that?
    Yep they can throw any exception you want (just mention it in the throws clause if you want it to throw a checked exception (just as with any method)). Make sure that your constructor doesn't hook up the object somewhere or allocates resources before it throws the exception.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    HI, thanks a lot for your message!!

    But why this gives an error message while compling ??/


    class Test extends A{
    Test()throws Exception{
    System.out.println("Test10 Class");
    }
    Test(int i){}
    public static void main(String args[]) throws Exception{
    Test t = new Test();
    }
    }
    class A{
    A() throws Exception{
    System.out.println("A Class");
    }
    }

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

    Default

    Because If you declare that something throws an exception, you need to handle it. Consider going through the exception tutorials.

    Exception Tutorials

    Also, if you post code here that doesn't compile, please post the actual error message you're seeing. And if posting code, please use code tags (see link below).
    Last edited by Fubarable; 02-20-2011 at 02:53 PM.

  5. #5
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    unreported exception java.lang.exception, must be causge or declare to be thrown Test (int i){}

    This is the error message

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

    Default

    It looks like if one constructor throws an exception, all constructor overloads need to throw the same exception.

  7. #7
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    ya, it is something like that seems to be..
    But if it is a method, overload methods no need to consider about excptions ..!! isn't it

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    It looks like if one constructor throws an exception, all constructor overloads need to throw the same exception.
    No, the implicitly called super class constructor in the Test(int i) constructor can throw an Exception.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by JosAH View Post
    No, the implicitly called super class constructor in the Test(int i) constructor can throw an Exception.

    kind regards,

    Jos
    Thanks! !

  10. #10
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    Didn't get it where it does call implicitiy to Test (int i)?????

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by chathura87 View Post
    Didn't get it where it does call implicitiy to Test (int i)?????
    It's the other way around: every constructor calls either another constructor from the same class with the 'this( ... )' syntax or it calls a constructor from its superclass with the 'super( ... )' syntax. When your constructor does none of the two the compiler inserts a call to the no-args constructor of the superclass: super(). So the code for that Test(int i) constructor implicitly is:

    Java Code:
    Test(int i) {
       super(); // <--- generated by the compiler
       // ...
    }
    Because that constructor of the superclass can throw an exception and your constructor doesn't handle it, it should also throw that exception. Your constructor doesn't do any of them so your compiler whined about it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Architecture Rules 2.1.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-11-2008, 02:39 PM
  2. Architecture Rules 2.1.0
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-05-2008, 06:18 PM
  3. Replies: 1
    Last Post: 07-05-2008, 03:08 PM
  4. Architecture Rules 2.0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-17-2007, 02:03 PM
  5. Architecture Rules 2.0-rc2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-14-2007, 06:27 PM

Posting Permissions

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