Logs&Exceptions Flashcards
Zapis pridani loggeru, pkg
private static final Logger logger = Logger.getLogger(MyClass.getName());
java.util.logging
Nastaveni loggeru do file - nazev souboru + obsah (XML, warning)
logging.properties
.level=WARNING
handler=java.util.logger.FileHandler
java.util.logger.FileHandler.formatter=XMLFormatter
-//- .pattern=filenamePattern-%g.log
-//- .limit=1000
-//- .count=1 (rotace filu)
Nastaveni loggeru pro package “pkg” do konzole - nazev souboru + obsah (ne XML, info)
logging.properties
pkg.level=INFO
handler=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=SimpleFormatter
varianty log metody,
info(“message”)
log(Level.INFO, “message”)
log(Level.INFO, “message”, causeEx)
log(Level.INFO, “message {0}”, messageParam)
Parent vsech exceptions
Checked vs unchecked exceptions
Throwable
Checked - chytaji se a deklaruji povinne (nemusi byt v catch, pokud je finally)
Unchecked - potomci RuntimeException, nemusi se chytat a deklarovat
Co muze byt v catch 5
e.printStackTrace()
throw new Exception(e)
Throwable ex = e.getCause()
Throwable[] exs = e.getSupressed()
return; - finally se provede, ale metoda nepokracuje
Try-with-parameters - priklad zapisu
+ kdo tam smi byt
+ co se stane, pokud “finally” vyhodi vyjimku
try (BR br = new BufferedReader; PW pw = new PrintWriter()) { … } catch …
AutoCloseable (maji metodu close())
je potlacena, lze ji ziskat z exception.getSuppressed()
Typy handleru (5), typy formatteru (2)
Console
File
Memory
Socket
Stream
SimpleFormatter, XmlFormatter
Logger metody: nastaveni urovne - je uroven alespon ? - vstup, vystup z metody - vyjimka
setLevel(Level.SEVERE)
isLoggable(Level.SEVERE)
entering, exiting
throwing
metody pro precise log (class a metoda) a resource bundle log
logp
logrb
syntaxe tvorby vlastni vyjimky
public class MyException extends Exception() {
// konstruktory, ktere volaji super
}
Assertions:
jak je zapnout
syntax s a bez error message
co se stane, pokud failne
-ea (enable assertions) jako parametr pro jvm, defaultne jsou vyple
assert boolVar;
assert boolVar: errorMsg;
program skonci
JVM parametr pro verbose NullPointerException
-XX:+ShowCodeDetailsInExceptionMessages
Return v catch
Return ve finally
catch: provede se finally a pak metoda skonci
finally: metoda skonci. Pokud byl return v catch, muj ho prepise