You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by temp temp <mi...@yahoo.com> on 2006/11/10 16:54:49 UTC

Java exception handling

    For good  exceptional handling  should I create  all my methods in try catch block and throw  exception in the catch block with e.printstacktrace()
       
      Exammple
       
                  public void processQueryResults(Map  metaInfo, Map entityInfo) throws WorkflowException {
                               try{
                                           if(entities==null){
                                                       entities=  new ArrayList();
                                           }
                                           IEntityDAO  entityDAO  =ObjectFinder.getEntityDAOFromBpAlias(StringUtilities.stripNamespace(metaInfo.get(IWorkflow.BUSINESS_PROCESS_ALIAS).toString()));
                                           Entity  entity=entityDAO.createNew();
                                           Integer  entityId=new Integer(metaInfo.get(IWorkflow.ENTITY).toString());
                                           entity.setId(entityId.intValue());
                                           entity.fromMap(entityInfo);
                                           entity.setMetaInfo(metaInfo);
                                           entities.add(entity);
                               }catch(Exception  e)      {
                                           e.printStackTrace();
                                           throw new  WorkflowException(e);
                               }
                  }
  Is this good way to handle exception ?
  Thanks & Regards
  miro
  
    
 
---------------------------------
Access over 1 million songs - Yahoo! Music Unlimited.

Re: Java exception handling

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Miro,

temp temp wrote:
> For good  exceptional handling  should I create  all my methods in
> try catch block and throw  exception in the catch block with
> e.printstacktrace()

I would recommend simply doing this:

> }catch(Exception  e) {
>          throw new  WorkflowException(e);
> }

Don't bother printing the stack trace.

Also, you might not want to catch /all/ Exceptions, but instead catch
those exceptions which are actually thrown. Of course, it is possible
that some methods you call are declared to "throws Exception", in which
case, you don't really have a choice.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFVKxN9CaO5/Lv0PARAoKeAJ40D4TVjiyWLuQKrofS5R6Ofv4VKQCgsKTR
ZD5VL1wDK9OLIhh1FbfilbA=
=cLCI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org