|
|
|
![]() |
|
Strumenti |
![]() |
#1 | |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
[JAVA] Strano comportamento Try-Catch-Finally
Posto un estratto di codice:
Codice:
try { ... fs = new FileInputStream(this.resource.getResourceFile()); ... while (this.isActive==true) { if ((line = input.readLine()) != null) { try { ... this.persist(parsable); ... this.sendJMSMessage((JMSPublishable)parsable); ... continue; } catch (Exception e) { logger.error("Si e' verificata una eccezione durante la persist o la sendJMS del seguente messaggio: "+line, e); } Thread.sleep(1000L); ... } ... } } catch (SAXException x) { logger.error("SAXException while creating the PatternMatchingHandler",x); } catch (ParserConfigurationException x) { logger.error("ParserConfigurationException while creating the PatternMatchingHandler",x); } catch (InterruptedException x) { logger.error("The thread has been interrupted",x); } catch (FileNotFoundException fnf){ logger.error("A FileNotFoundException has been raised. It could be a temporary log being destroyed",fnf); this.isActive = false; } catch (IOException ioe) { logger.error("A generic IOException has been raised",ioe); } catch (Exception e) { logger.error("Failsafe generic Exception catching",e); } finally { try { // clean everything ... if(fs!=null){fs.close();} } catch (Exception e) { logger.error("Some cleaning up in the finally block didn't work",e); } if (this.isActive==true){ logger.fatal("A fatal exception has been caught, the whole process is about to be killed"); System.exit(1); } else { logger.warn("The thread is about to die. This could be normal in case of a thread running on a temporary log"); } } Quote:
Codice:
while (this.isActive==true) Codice:
this.isActive==true Codice:
} catch (Exception e) { logger.error("Failsafe generic Exception catching",e); } finally { .... Grazie a tutti per l'aiuto, TD |
|
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jul 2009
Città: Varès
Messaggi: 658
|
beh ma la finally viene eseguita anche se entra in uno dei blocchi catch... non è come il default di uno switch che viene eseguito solo se non intercettato prima.
dovresti controllare nel logger.error per capire quale eccezione è stata lanciata... oppure mettere una print dell'eccezione nello stesso if che ti da la stampa che riscontri. l'unica cosa che è sicura è che viene lanciata un eccezione durante l'attività dell'oggetto... |
![]() |
![]() |
![]() |
#3 | |||
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Quote:
Codice:
} catch (Exception e) { logger.error("Failsafe generic Exception catching",e); } finally { .... Quote:
Quote:
|
|||
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
bumpino....
![]() |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Prova a rilanciare ogni eccezione catturata nelle varie clausole catch per vedere quale sia quella che effettivamente viene sollevata (e che non viene loggata?) e/o stampala sullo standard output.
2 Domande banali: 1. dove viene settato a false isActive? 2. c'è solo un thread che accede in scrittura a isActive?
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Quote:
|
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Ieri sera ero un po rinco...
isActive effettivamente non viene mai modificato. Per quanto riguarda la storia delle print ho pensato che, se entra dentro catch (Exception e), e prova a stamparla ma questa è per qualche motivo null (return nel blocco finally di un metodo precedente?), allora avrei una nullpointerexception che mi fa andare dritto nel finally, giusto? Ora provo il tuo consiglio dei throw, intanto ieri ho messo delle print senza stampare anche il throwable subito prima di quelle che stampano anche il throwable, queste print di solo testo non possono in alcun modo fallire penso, quindi almeno una dovrebbe comparire... |
![]() |
![]() |
![]() |
#8 | ||
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Nuovo tentativo...
Codice:
try { ... while (true) { ... super.persist(parsable); super.sendJMSMessage((JMSPublishable)parsable); ... } } catch (SAXException x) { super.logger.fatal("SAXException"); super.logger.error("SAXException while creating the PatternMatchingHandler",x); } catch (ParserConfigurationException x) { super.logger.fatal("ParserConfigurationException"); super.logger.error("ParserConfigurationException while creating the PatternMatchingHandler",x); } catch (InterruptedException x) { super.logger.fatal("InterruptedException"); super.logger.error("The thread has been interrupted",x); } catch (FileNotFoundException fnf){ super.logger.fatal("FileNotFoundException"); super.logger.error("A FileNotFoundException has been raised. It could be a temporary log being destroyed",fnf); } catch (IOException ioe) { super.logger.fatal("IOException"); super.logger.error("A generic IOException has been raised",ioe); } catch (Exception e) { super.logger.fatal("Exception"); super.logger.error("Failsafe generic Exception catching",e); } finally { try { super.cleanUp(); if(input!=null){input.close();} if(fileReader!=null){fileReader.close();} if(fs!=null){fs.close();} } catch (Exception e) { super.logger.fatal("Exception nel finally()"); super.logger.error("Some cleaning up in the finally block didn't work",e); } super.logger.fatal("A fatal exception has been caught, the whole process is about to be killed"); System.exit(1); } Quote:
Quote:
|
||
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Temo di essermi perso qualcosa:
Codice:
finally { ... super.logger.fatal("A fatal exception has been caught, the whole process is about to be killed"); System.exit(1); }
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
![]() |
![]() |
![]() |
#10 |
Member
Iscritto dal: Aug 2005
Messaggi: 168
|
Non ho capito una cosa... Ti è chiaro il fatto che il programma entra sempre nella finally, sia che ci sia un'eccezione sia che non ci sia?
|
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Non ho capito cosa intendi sul perchè la scrivo nel log, ti dico perchè uccido la virtual machine. In teoria il thread sta in while(true), non dovrebbe mai smettere di lavorare. Qualora venga presa una di quelle eccezioni (ci sono try catch piu interni per gestire eccezioni in altri punti) il thread deve terminare. In più, invece di avere un thread "worker" in meno e far proseguire l'applicazione con gli altri, viene attualmente preferito far crashare l'applicazione e farla ripartire in automatico.
Ultima modifica di tylerdurden83 : 15-06-2010 alle 16:07. |
![]() |
![]() |
![]() |
#12 | |||
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Quote:
Codice:
try{ while(true){ doStuff(); } } catch (Exception e){ stampa("Eccezione!"); } finally{ stampa("Finally chiamato"); } Quote:
Quote:
Ultima modifica di tylerdurden83 : 15-06-2010 alle 16:08. |
|||
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:42.