The Artima Developer Community
Sponsored Link

Java Answers Forum
Exception Handling

2 replies on 1 page. Most recent reply: Jun 6, 2005 12:50 PM by Archie

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Archie

Posts: 2
Nickname: archie123
Registered: Jun, 2005

Exception Handling Posted: Jun 5, 2005 3:04 PM
Reply to this message Reply
Advertisement
I want to modify the following program so that the user can continue entering strings after the exception message appears. How do I do that???

import java.util.Scanner;

public class StringTooLongExceptionDriver
{
public static void main(String[] args) throws StringTooLongException
{
String strInput;

StringTooLongException error = new StringTooLongException("String longer than 20 characters");

Scanner scan = new Scanner(System.in);

System.out.print ("Enter some characters (DONE to quit): ");
strInput = scan.nextLine();

while (!strInput.equals ("DONE"))
{
if (strInput.length() > 20)
throw error;

System.out.print ("Enter some characters (DONE to quit): ");
strInput = scan.nextLine();
}
}
}

public class StringTooLongException extends Exception
{
StringTooLongException (String message)
{
super (message);
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exception Handling Posted: Jun 5, 2005 10:54 PM
Reply to this message Reply
The while statement must be outside the method wich throws the error.


{
  try {
    getUserInput(); //read String, throw Exception if String > 20
  } catch (StringTooLongException e) {
//    handle Exception
  }
} while (!endCondition);

Archie

Posts: 2
Nickname: archie123
Registered: Jun, 2005

Re: Exception Handling Posted: Jun 6, 2005 12:50 PM
Reply to this message Reply
Thanks for your reply...it really helped!!!

Flat View: This topic has 2 replies on 1 page
Topic: Association Problem Previous Topic   Next Topic Topic: How to fetch data from TV tuner card

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use