java - Give (an) example(s) of something bad that happens with a 'catch(Exception)' clause -


this follow question this one.

it seems accepted doing broad 'catch (exception)' bad idea.

the reasoning typically 'you must handle exception properly' , 'catch can handle' , few other generic sounding arguments.

those generic answer sound reasonable, don't satisfy me.

let me concrete. here's typical bit of code supposed 'bad'.

try {    ... something... } catch (exception e) {    log(e); //leave trace debugging    return ...a value context can deal with... } 

a sceptical mind can remain unconvinced not handling exception, (which blow entire program), better outcome handling in generic way.

so specific , convincing example(s), code snippet(s), of bad happens because of supposedly broad catch clause 1 above.

ps: 1 think question in other languages java, since want specific examples, specific exceptions might raised java program , jre.

ps2: interested in examples of exception in fact subtype of java.lang.exception , not more broadly 'throwable'. rules out stuff 'outofmemoryerror' valid examples.

catching nullpointerexception can result in application stumbling onward oblivious fact needed not done. later application fail again or behave abnormally because previous operation did not complete, harder figure out now.

eating exception within transaction can result in transaction not getting rolled when should have been. end inconsistent data.

eating interruptedexception:

public void run() {     while (!thread.currentthread().isinterrupted()) {         try {             dosomework();             thread.sleep();         } catch (exception e) {         }     } } 

if thread gets interrupted while sleeping, interrupted flag reset when sleep method throws interruptedexception, since catch doesn't restore interrupt flag while loop condition stays false , thread doesn't exit.

similarly, since interruptedioexception subclass of ioexception: if have network io code handles ioexception can fail restore interrupt flag on , fail detect thread has been interrupted.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -