Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By sunde887
  • 1 Post By Toll

Thread: Creating Exceptions

  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Creating Exceptions

    If I create an Exception like this:
    Java Code:
    package error;
    
    public class NoPieceException extends Exception{
    
    	String msg;
    
        public NoPieceException()
        {
            msg = new String("There is no piece at that location.");
        }
    }
    I have a string in the constructor of the exception. How would I call that out if throwing this exception? I know i can print the stack trace. But that doesn't do it.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Make a function in it called getMessage that returns it?

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Then i think i am confused on why you would need anything in an exception. What goes into a normal Exception?
    [Edit] I want it so that when the exception is throw it throws that string. btw

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    How do you mean, "it throws that string"? You could make a toString() in your exception that returns that String, which means "System.out.println(exception)" will print that String, if that's what you're after.

  5. #5
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Well say I am making something that i have to check if something is there. If nothing is there it throws the exception which then prints out that string from the exception. I am not sure how else to explain it. Is there any reason to have strings and other things inside of the exception? or are exceptions normally empty?

  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

    You should probably just pass the constructor message to the super constructor.

    It's helpful to create custom exceptions with variables if you want the exception to do something unique, if however, you just want a more fitting name which returns the problem, just pass the string to supers constructor.
    rizowski likes this.

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Exceptions normally have messages in them. The type of exception gives you a general idea what is wrong, while the message gives more details.
    rizowski likes this.

  8. #8
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Ah Alright that Helped.
    I ended up doing this:
    Java Code:
    package error;
    
    public class NoPieceException extends Throwable{
    	
        public NoPieceException(String msg){
        	super(msg);
        }
    }

Similar Threads

  1. Exceptions
    By Maya in forum New To Java
    Replies: 2
    Last Post: 05-24-2011, 06:30 AM
  2. Creating new exceptions.
    By carman12 in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 12:56 AM
  3. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 01:44 PM
  4. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 09:06 PM
  5. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 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
  •