Results 1 to 4 of 4
  1. #1
    stevemcc is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default [SOLVED] Handle own exception

    I have an assignment to make my own exception. The exception need to be thrown when the user enters a string longer than 31 characters. Then next, task and the one I am struggling with is to do the same, but this time correctly handle the exception gracefully.

    StringTooLongException.java
    Java Code:
    public class StringTooLongException extends Exception {
    	
    	public StringTooLongException(String message) {
    		super(message);
    	}
    	
    }
    TooLongCrashes.java
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class TooLongCrashes {
    
    	public static void main(String[] args) throws StringTooLongException, IOException {
    		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    	    String message;
    		boolean c = true;
    		while(c) {
    			stdin = new BufferedReader(new InputStreamReader(System.in));
    			message = stdin.readLine();
    			if(message.length() >= 32)
    				throw (new StringTooLongException(message));
    			else
    				System.out.println(message);
    			
    		}
    
    	}
    
    }
    TooLogHandled.java
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class TooLongHandled {
    
    	static TooLongCrashes too;
    		
    	public static void main(String[] args) throws StringTooLongException, IOException {
    		try {
    		too = new TooLongCrashes();
    		} catch(Exception e) {
    			System.out.println(too.toString());
    		}
    	}
    }
    In TooLongHandled.java I cannot seem to figure out how I should go about handling the exception.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Hi steve. All you need to do is:
    Java Code:
    public class StringTooLongException extends Exception {
        
        public StringTooLongException(String message) {
            throw new IllegalArgumentException("Exception: String too long.");
        }
        
    }
    With this, the output is below on an input of "Hello":
    Hello
    Hello
    But on input longer than 32 you see this:
    123456789012345678901234567890123
    Exception in thread "main" java.lang.IllegalArgumentException: Exception: String too long.
    at StringTooLongException.<init>(StringTooLongExcepti on.java:4)
    at TooLongCrashes.main(TooLongCrashes.java:17)
    Java Result: 1
    Should this satisfy your question, please mark this thread as 'Solved', if not, definitely let us assist with more on this topic. :)

    My bad.. I didn't see that it actually worked before I made an adjustment.. so, basically you picked up another way to throw an exception. :)
    Last edited by CaptainMorgan; 04-10-2008 at 05:51 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Steve may be worried, on his code with the exception just the user input string is displayed. May be looking to make much descriptive exception message as Captain says.

Similar Threads

  1. How to handle socket Exception
    By mayank0512 in forum Networking
    Replies: 14
    Last Post: 12-22-2010, 12:31 AM
  2. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  3. Better way to handle exceptions
    By javaplus in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2008, 07:47 PM
  4. handle wrong input int/null
    By Camden in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 10:37 PM
  5. how to handle exceptions
    By paty in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 05:17 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
  •