Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-01-2011, 05:21 AM   PM User | #1
Xibe
New Coder

 
Join Date: Nov 2005
Posts: 66
Thanks: 8
Thanked 0 Times in 0 Posts
Xibe is an unknown quantity at this point
Arrow Exception Closes Program!

I'm trying to create a program for class that throws an exception if the string entered is longer than 20 characters. Once the exception is thrown, it is supposed to continue and say: "Do you want to enter another line?" But, it keeps ending prematurely after the exception is thrown.

Here's my program:
Code:
/*
CS182
Excercise 5
Ashley Glasser
4/30/2011
 */
// Excercise 5 MessageTooLong.java
import java.util.Scanner;
public class MessageTooLong extends Exception {
        public static void main(String args[])
        throws MessageTooLongException
	{

		Scanner keyboard = new Scanner(System.in);
 String line;
 char preference;
 int length;
boolean go = true;
    while (go) {
 System.out.println("Enter a line of text.");
 System.out.println("Use no more than 20 characters.");
 line = keyboard.next();
length = line.length();
     if (length <= 20)
                {
                    System.out.println("You entered " + length + " characters, which is an acceptable length.");
                     System.out.println("Would you like to enter another line?");
 System.out.println("Enter 'y' to continue or 'n' to quit.");
        preference = keyboard.next().charAt(0);
        if ((preference == 'y') || (preference == 'Y')) {
            go = true;
        } else {
            go = false;
        }
                }
      if (length > 20)
                {
                     throw new MessageTooLongException();
                }

        }
    }
}
And here's my exception:
Code:
public class MessageTooLongException extends Exception
{
	public MessageTooLongException()
	{
		super("Message Too Long!");
	}

	public MessageTooLongException(String message)
	{
		super(message);
		System.out.println("Message Exception invoked with an argument.");               
	}

}
Why is it closing out the program?
Xibe is offline   Reply With Quote
Old 05-01-2011, 07:04 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Of course, the main method is throwing an exception so you're JVM has to deal with it.
I don't really see a purpose of tossing an exception at all. Since you would require the code to check if it has entered >= 20 in the first place, the exception becomes pointless since you would throw it and catch it yourself anyway. If you were to use this in something like a JDialog, than that would make a little more sense since it will be handled outside of the controlling program.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:12 PM.


Advertisement
Log in to turn off these ads.