[3202] 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 21:53:39 1995

To: Thomas Ball <Thomas.Ball@Eng.Sun.COM>
Date: Thu, 2 Nov 1995 18:30:45 -0500 (EST)
From: Edith Au <edith@pencom.com>
Cc: java-interest@java.sun.com
In-Reply-To: <9511022218.AA17338@alapa.Eng.Sun.COM> from "Thomas Ball" at Nov 2, 95 02:18:17 pm

> >   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()
> >   {}
> > }
> > 
> > 
> 
> I think your confusion stems from not fully understanding how to use
> exceptions -- it's a new concept for many programmers, so please don't
> take my comment as a criticism.  
  No, I won't.  I appreciate your help very much! :)


Exceptions are useful for separating
> correct program execution from error handling -- it makes for easier to
> read code if used properly, as well as fewer ignored error conditions.
> Several recent C++ books discuss exception handling for people who are
> new to the concept -- can someone else recommend such a book?
> 

  I think my example is too simple.  What if, my main() calls X.a(), X.a()
then calls Y.b() and 
class Y
{
  boolean b(int x)
  {
    if (x < 0 || x> 10)
      printErrMesg("mesg");
      return false;
    else
      /* do something ... */

      return true;
  }
}

  Using your suggestion, I need to signal X.a() that something goes wrong 
and then have X.a() to signal main().  But if I use exception,
class Y
{
  void b(int x) throws outOfRangeException
  {
    if (x < 0 || x> 10)
       throw new outOfRangeException()
    /* do something ....*.
  }
}

I can take care of the out of range problem in a nice way.  I can have X.a()
to throw the exception back to the main() and let it handle the exception. 
So, I can do the following in main()
  

  while (true)
  {
 
    try   
    {
      ......
      X.a(..);
      ....
    }
    catch (typeA_exception e)
    {
      break;  /* serious problem.   Not a good idea to continue.  
				Kill the thread */
    }
    catch (outOfRangeException e)
    {
      continue; /* just a minor problem.  Redo the whole process again */
    }
    catch (Exception e)
    {
      print ("Programmer, you forgot to catch your exception!");
      break;
    }
  }

  
  Being a C programmer, maybe my reasoning is not correct in the OO world.
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.  

  I think I still don't understand exception......


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