Java 1.6 : Learn how to handle exceptions -



Java 1.6 : Learn how to handle exceptions -

i did extensive research on exceptions, i'm still lost. i'd know or not.

and i'd give me expert sentiment on next illustration :

public void myprocess(...) { boolean error = false; seek { // step 1 seek { startprocess(); } grab (ioexception e) { log.error("step1", e); throw new myprocessexception("step1", e); } // step 2 seek { ... } grab (ioexception e) { log.error("step2", e); throw new myprocessexception("step2", e); } grab (dataaccessexception e) { log.error("step2", e); throw new myprocessexception("step2", e); } // step 3 seek { ... } grab (ioexception e) { log.error("step3", e); throw new myprocessexception("step3", e); } grab (classnotfoundexception e) { log.error("step3", e); throw new myprocessexception("step3", e); } // etc. } grab (myprocessexception mpe) { error = true; } { finalizeprocess(error); if (!error) { log.info("ok"); } else { log.info("nok"); } } } is ok throw personnal exception (myprocessexception) in each step in order manage global try...catch...finally ? is ok manage each known exception each step ?

thank help.

edit 1 :

is practice ? log straight in global grab getting message, , try...catch(exception) in upper level....

the purpose stop if step fail, , finalize process (error or not).

in controller public void callprocess() { seek { myprocess(...); } grab (exception e) { log.error("unknown error", e); } } in service public void myprocess(...) { boolean error = false; seek { // step 1 seek { startprocess(); log.info("ok"); } grab (ioexception e) { throw new myprocessexception("step1", e); } // step 2 seek { ... } grab (ioexception e) { throw new myprocessexception("step2", e); } grab (dataaccessexception e) { throw new myprocessexception("step2", e); } // step 3 seek { ... } grab (ioexception e) { throw new myprocessexception("step3", e); } grab (classnotfoundexception e) { throw new myprocessexception("step3", e); } // etc. } grab (myprocessexception mpe) { error = true; log.error(mpe.getmessage(), mpe); } { finalizeprocess(error); if (!error) { log.info("ok"); } else { log.info("nok"); } } }

thank you.

edit 2 :

is real bad practice grab (exception e) in lower level , throws personnal exception ?

doesn't exist generic rule,it depends on needs. can throw personal exception, , can manage each known exception.

but pay attention, of import want.

try{ exec1(); exec2(); // if exec1 fails, not executed }catch(){} try{ exec1(); }catch(){} try{ exec2(); // if exec1 fails, executed }catch(){}

java exception

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -