Results 1 to 8 of 8
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default overriding ToString()

    Can someone explain to me how to override the toString(method) in an Exception class? My instructor tried explaining it to me but it kind of went above my head. Sample code below.
    Java Code:
    public class MetalR0'sException extends Exception {
    	public MetalR0'sException(String message) {
    		super(message);
    	}
    	public MetalR0'sException() {
    		super("This message should print when exception is thrown");
    	}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Exactly the same as you would override toString in any other class, since an exception is just a class.

  3. #3
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    So I would just add a public String to String method to the above code and set the return String equal to the message I want the error to have? ie

    public String toString() {
    String s = "This message should print ....."
    return s;
    }

    Right?
    Last edited by MetalR0; 08-04-2011 at 05:49 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yup. You could also just do
    Java Code:
    return "the message";

  5. #5
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Got it. Thanks!

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'd like to add that any time you override any method you should use the override annotation. So toString will look like this:

    Java Code:
    @Override public String toString(){
      return "";
    }
    This same @Override annotation can be used anywhere and it will allow you to get compile time errors when you improperly override a method, without this method you may just get strange behavior, take the following as an example

    Java Code:
    public String toStrng(){
      return "";
    }
    This will compile but when you try printing an object you get something like

    Class@152af7

    Which is the default toString, with the override annotation, an error will occur signaling your missed i

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Why do you need to override the toString() method?
    At least for that example above, the existing one (on Throwable) already supplies the message text.

    And that class name is illegal.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    I'd like to add that any time you override any method you should use the override annotation.
    I never do that; Eclipse will tell me if the method actually overrides its super implementation ... I trust that ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Confused about Overriding - Please Help
    By jazzermonty in forum New To Java
    Replies: 15
    Last Post: 01-24-2011, 06:20 PM
  2. overriding v/s hiding
    By parminder in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 05:24 AM
  3. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 08:47 AM
  4. overriding toString method
    By matin1234 in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 05:35 AM
  5. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 09:45 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
  •