[3167] in java-interest
[Fwd: Better way to handle user-defined exception
daemon@ATHENA.MIT.EDU (Edith Au)
Thu Nov 2 01:37:21 1995
To: java-interest@java.sun.com
Date: Wed, 1 Nov 1995 23:06:44 -0500 (EST)
From: Edith Au <edith@pencom.com>
Hi,
I posted this message couple days ago but no one response to it.
Can someone please help?
> The following "code" will throw a user-defined exception if
>
> user entered a number < 0 or > 10.
>
> class abc
> {
>
> public static void main(String args[]) throws
> outOfRangeException
> {
> try
> {
> int x = new Integer(args[0]).intValue();
> if (x < 0 || x > 10)
> throw new outOfRangeException();
> }
> catch (NumberFormatException e)
> {
> printErrMesg("Wrong format");
> }
> catch (outOfRangeException e)
> {
> printErrMesg("Out of range input");
> }
> }
>
> static void printErrMesg(String mesg)
> {
> System.out.println(mesg);
> }
> }
>
>
> class outOfRangeException extends Exception
> {
> outOfRangeException()
> {}
> }
>
>
> From the user friendly point of view, I don't want to show user
> a thread stack if an out of range number is entered. I just
> want to print out a simple error message. I am forced to
> create a dummy exception subclass, outOfRangeException, because
> I want to catch the exception in the main program.
>
> You might argue I can just simply do
> if (x<0 || x>10)
> System.out.println ("Out of Range Input.");
>
> but that's not what I want. I would like to have a centralize
> place (method printErrMesg()) to handle error message.
>
> Does anyone has a better way to handle this? Creating a dummy
> exception is kind of ugly and confused.........
>
>
> Cheers,
> Edith
>
==============================================================================
Edith Au Tel: (212) 513 7777
WWW Specialist Email: edith@pencom.com
Pencom Systems Incorporated WWW: http://www.pencomsi.com/~edith
40 Fulton Street,
NY, NY 10038
===============================================================================
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com