Java exception handling in non sequential tasks (pattern/good practice)

Posted by Hernán Eche on Stack Overflow See other posts from Stack Overflow or by Hernán Eche
Published on 2010-04-23T16:16:28Z Indexed on 2010/04/23 16:23 UTC
Read the original article Hit count: 465

There are some task that should't be done in parallel, (for example opening a file, reading, writing, and closing, there is an order on that...)

But... Some task are more like a shoping list, I mean they could have a desirable order but it's not a must..example in communication or loading independient drivers etc..

For that kind of tasks, I would like to know a java best practice or pattern for manage exceptions..

The java simple way is:

 getUFO {
      try {
            loadSoundDriver();
            loadUsbDriver();
            loadAlienDetectorDriver();
            loadKeyboardDriver();    
  } catch (loadSoundDriverFailed) {
     doSomethingA;
  } catch (loadUsbDriverFailed) {
      doSomethingB;
  } catch (loadAlienDetectorDriverFailed) {
      doSomethingC;
  } catch (loadKeyboardDriverFailed) {
      doSomethingD;
  } 
}

But what about having an exception in one of the actions but wanting to try with the next ones??

I've thought this approach, but don't seem to be a good use for exceptions I don't know if it works, doesn't matter, it's really awful!!

getUFO {
       Exception ex=null;
 try {
       try{  loadSoundDriver();
       }catch (Exception e)  {  ex=e; }
       try{  loadUsbDriver();
       }catch (Exception e)  {  ex=e; }
       try{ loadAlienDetectorDriver();
       }catch (Exception e)  {  ex=e; }
       try{  loadKeyboardDriver()
       }catch (Exception e)  {  ex=e; }
      close the file;
       if(ex!=null)
       { throw ex;
        }
  } catch (loadSoundDriverFailed) {
     doSomethingA;
  } catch (loadUsbDriverFailed) {
      doSomethingB;
  } catch (loadAlienDetectorDriverFailed) {
      doSomethingC;
  } catch (loadKeyboardDriverFailed) {
      doSomethingD;
  } 
}

seems not complicated to find a better practice for doing that.. I still didn't

thanks for any advice

© Stack Overflow or respective owner

Related posts about java

Related posts about exception