[3205] in java-interest

home help back first fref pref prev next nref lref last post

Re: Better way to handle user-defined exception

daemon@ATHENA.MIT.EDU (Edith Au)
Thu Nov 2 22:01:14 1995

To: Thomas Ball <Thomas.Ball@Eng.Sun.COM>
Date: Thu, 2 Nov 1995 20:00:27 -0500 (EST)
From: Edith Au <edith@pencom.com>
Cc: java-interest@java.sun.com
In-Reply-To: <9511022352.AA17477@alapa.Eng.Sun.COM> from "Thomas Ball" at Nov 2, 95 03:52:12 pm

> 
> >  Let say if method1 calls method2 ....calls method5.  I thought throwing
> >  an exception in method5 and have method1 or method2 to catch it will make
> >  method3, method4 's codes cleaner because they don't need to do anything
> >  special in their codes.  
> 
> That's exactly right -- throw the exception where the problem is detected
> (method5), and then handle the exception where it's most appropriate
> (method1).  You understand this better than you give yourself credit for.
> 
> The only wrinkle the new compiler adds is that the intermediate methods
> need to declare that they can throw method5's exception, since they
> aren't dealing with that exception in any way:
> 
> void method5(int x) throws OutOfRangeException {
> 	if (x > 10) {
> 		throw new OutOfRangeException("x = " + Integer.toString(x));
> 	}
> }
> 
> void method2(int x) throws OutOfRangeException {
> 	method5(x);
> }
> 
> void method1(int x) {
> 	try {
> 		method2(x);
> 	} catch (OutOfRangeException e) {
> 		System.out.println(e.toString());
> 	}
> }

OK.  Now back to my original question.  I still need a dummy exception,
OutOfRangeException which has no code at all because I don't want to show 
user the thread stack (by calling super(mesg) in a constructor) and I would
like to have a separate method to handle the error message.

I might end up with something like

main()
{

  try
  {
   ....
   ....
    
  }
  catch (dummyException1 e)
  {
    printErrMesg("invalid ID.  Please try again.");
    ...
  }
  catch (dummyException2 e)
  {
    printErrMesg("Please enter a whole number for the age field.");
    ....
  }
  catch (dummyException3 e)
  {....}
  .
  .
  .

  There must be a better way to handle this.  The reason why I insist
having printErrMesg to hanlde the error message is because I might want
to print error to System.in, to the status bar of a browser, to the network
stream, to a popup windows......

  Any good suggestion?

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

home help back first fref pref prev next nref lref last post