gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > Correct and clean exception handling

#121281 - Mr Snowflake - Sun Mar 11, 2007 2:46 am

For the same project as in my previous topic I, obviously, need exception handling. But I don't know how to handle, cleanly, a specific situation: Say I have a function:
Code:

function toBeCalled() throws SomeException{
  for(String name : nameList)
    if(name.compareTo("") == 0)
      throw new SomeException();
}

If I call this function from an other function, and I need to keep toBeCalled() looping through nameList even if an exception is encountered (in this case, I just want to display a message saying name is empty, but it's not critical, so I can continue). How should I do it?
- I could catch the exception in toBeCalled(), and then jump back in the for lus with goto. But this is really bad and with this foreach loop, I don't even think it is possible to pickup where I left. but then there's the problem that I do not know how to output the error and if to output the error when in toBeCalled().
- I could keep a Vector with all the exceptions generated in toBeCalled(), but for some reason I don't really like this and I prefer having the exceptions outputted immediately.

In my project I use the foreach loop. And it is a Java project.

And to improve my English writing: How is my English?? :)
_________________
http://www.mrsnowflake.be

#121282 - tepples - Sun Mar 11, 2007 2:55 am

If you absolutely must use throw, then make a list of exceptions and throw that at the end of the function.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#121283 - sgeos - Sun Mar 11, 2007 3:17 am

Do you need to use throw at all? I guess I would do something like this:
Code:

error = 0;
foreach (...)
   if (test)
      // OK
   else
      error++;

if (0 != error)
{
   printErrorMessage(error);
   throw exception(error);
}


Your English is fine. Read quality English and practice if you want to improve your writing.

-Brendan

#121299 - keldon - Sun Mar 11, 2007 9:42 am

It is better not to print your error messages when you are throwing; and only catch an exception when you wish to handle it in some way, even if that means you are only catching it to deallocate anything that needs to deallocate and throw it again.

The problem many people make with exception handling is to catch exceptions that they don't wish to handle at the wrong time just so that their method does not throw an exception. You will often find that if you are catching an exception that the method that called you will also not get its result so it would have been easier if you threw that exception and passed on the responsibility of handling it to the one who should handle it.

Another thing to think about is the ACID properties. If you are in an operation such as saving and you have an exception then you don't want to have overwritten your previous file with an unfinished one. So just make sure that you will not end up in a bad state after having an exception.