Da WikiSitech.
|
|
| Riga 88: |
Riga 88: |
| | |- | | |- |
| | | valign="top" colspan="2" | | | | valign="top" colspan="2" | |
| − | ==Not Bad Practice but...2 stack trace== | + | ==Not Bad Practice but...== |
| | <code java> | | <code java> |
| | public class A { | | public class A { |
| Riga 107: |
Riga 107: |
| | A.f(); | | A.f(); |
| | } catch(TExcept t) { | | } catch(TExcept t) { |
| | + | // viene loggato tutto lo stackTrace per la seconda volta |
| | log.error("error", t); | | log.error("error", t); |
| | } | | } |
| Riga 114: |
Riga 115: |
| | |- | | |- |
| | | valign="top" colspan="2" | | | | valign="top" colspan="2" | |
| | + | |
| | ==Best Practice== | | ==Best Practice== |
| | Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione | | Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione |
Versione delle 12:38, 31 ott 2007
Log what is happened
Per avere un log 'utilizzabile' è necessario che l'eccezione venga loggata al massimo due volte:
- Chi intercetta l'eccezione originale deve loggare senza lo stacktrace e trasformarla in un eccezione applicativa
- Chi intercetta l'eccezione applicativa deve loggare l'eccezione completa di stacktrace
tutti gli altri punti in cui passa l'eccezione devono propagarla senza intercettarla e riloggarla nuovamente
Bad Practice.....case 1
public class A {
public static void f() throws TExcept {
try {
// Do something
} catch(Throwable t) {
// non viene loggato la causa dell'eccezione
throw new TExcept(t);
}
}
}
public class B {
void f1() {
try {
A.f();
} catch(TExcept t) {
// l'eccezione viene nascosta
}
}
}
|
Bad Practice.....case 2
public class A {
public static void f() throws TExcept {
try {
// Do something
} catch(Throwable t) {
// questo non è log...viene solo messo in console
t.printStackTrace();
throw new TExcept(t);
}
}
}
public class B {
void f1() {
try {
A.f();
} catch(TExcept t) {
// questo non è log...viene solo messo in console
t.printStackTrace();
}
}
}
|
Bad Practice.....case 3
public class A {
public static void f() throws TExcept {
try {
// Do something
} catch(Throwable t) {
// questo non è log...viene solo messo in console
System.out.println(t.getMessage();
throw new TExcept(t);
}
}
}
public class B {
void f1() {
try {
A.f();
} catch(TExcept t) {
// questo non è log...viene solo messo in console
System.out.println(t.getMessage();
}
}
}
|
Not Bad Practice but...
public class A {
public static void f() throws TExcept {
try {
// Do something
} catch(Throwable t) {
// viene loggato tutto lo stackTrace
log.error("error", t);
throw new TExcept(t);
}
}
}
public class B {
void f1() {
try {
A.f();
} catch(TExcept t) {
// viene loggato tutto lo stackTrace per la seconda volta
log.error("error", t);
}
}
}
|
Best Practice
Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione
|
Nel caso in cui l'eccezione sia di competenza di C
public class A {
public static void f() throws TExcept {
try {
// Do something
} catch(Throwable t) {
log.error(t.getMessage());
throw new TExcept(t);
}
}
}
public class B {
public static void f1() throws TExcept {
A.f();
}
}
public class C {
void f2() {
try {
B.f1();
} catch(TExcept t) {
log.error("error", t);
}
}
}
|
Nel caso in cui l'eccezione sia di competenza di B
public class A {
public static void f() throws TExcept {
try {
// Do something
} catch(Throwable t) {
log.error(t.getMessage());
throw new TExcept(t);
}
}
}
public class B {
public static void f1() {
try {
A.f();
} catch(TExcept t) {
log.error("error", t);
}
}
}
public class C {
void f2() {
B.f1();
}
}
|
|