Skip to content

Commit 8311ec0

Browse files
committed
Merge branch 'gcg18000'
2 parents 6999016 + a323449 commit 8311ec0

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Exceptions und Prozessbeendigung
2+
3+
## Verwende keine Exceptions statt `if...else`-Statements (GCG18001) <img src="/img/2.png" alt="recommendation level 2" />
4+
Exceptions sollten nicht verwendet werden, um Entscheidungen zu treffen, sondern
5+
um Fehler entsprechend ihrer Ursache und möglichen Auswirkungen zu behandeln.
6+
7+
Diese Regel kombiniert u. A. die Regeln `GCG18002` und `CGC18003`.
8+
9+
## Werf keine Exceptions die direkt wieder gefangen werden (GCG18002) <img src="/img/1.png" alt="recommendation level 1" />
10+
```
11+
// not okay!
12+
class Foo {
13+
public void AnyMethod() {
14+
try {
15+
throw new Exceptions();
16+
} catch {
17+
// do something
18+
}
19+
}
20+
}
21+
22+
// also not okay
23+
class Foo {
24+
public void AnyMethod() {
25+
try {
26+
MethodThrowingException();
27+
} catch {
28+
// do something
29+
}
30+
}
31+
32+
private void MethodThrowingException() {
33+
throw new Exception();
34+
}
35+
}
36+
37+
// maybe okay
38+
class Foo {
39+
public void AnyMethod() {
40+
try {
41+
MethodThrowingException();
42+
} catch {
43+
// do something
44+
}
45+
}
46+
47+
public void MethodThrowingException() {
48+
throw new Exception();
49+
}
50+
}
51+
52+
// okay!
53+
class Foo {
54+
public void AnyMethod() {
55+
try {
56+
new Bar().MethodThrowingException();
57+
} catch {
58+
// do something
59+
}
60+
}
61+
}
62+
63+
class Bar {
64+
public void MethodThrowingException() {
65+
throw new Exception();
66+
}
67+
}
68+
```
69+
70+
## Statt NullPointer-Exceptions zu fangen, prüfe sie vorzeitig ab (GCG18003) <img src="/img/1.png" alt="recommendation level 1" />
71+
NullPointer-Exceptions entstehen, wenn ein Objekt einen nicht erwarteten Zustand
72+
hat. Statt im Prozess solche Exceptions abzufangen, sollten sie früh erkannt und
73+
entsprechend behandelt werden.
74+
75+
## Werf eine Exception, statt eine Art von Status-Code zurückzugeben (GCG18004) <img src="/img/2.png" alt="recommendation level 2" />
76+
Eine Codebasis die Rückgabewerte verwendet um einen Status (erfolgreich oder
77+
fehlgeschlagen) zu kommunizieren, tendiert dazu, viele verschachtelte
78+
`if`-Statements überall verteilt zu haben. Oft wird auch vergessen, den
79+
Rückgabewert ordentlich zu prüfen.
80+
Geworfene Exceptions können abgefangen oder durch eine genauere Exception
81+
ersetzt werden, um sie an auf einer höheren Ebene zu behandeln. In den meisten
82+
Systemen ist es normal, dass Exceptions immer dann geworfen werden, wenn eine
83+
unerwartete Situation auftaucht.
84+
85+
## Werf eine möglichst genaue Exception (GCG18005) <img src="/img/2.png" alt="recommendation level 2" />
86+
Wenn es möglich ist, eine Exception von einem spezifischen Typ zu werfen,
87+
dann sollte ein Typ gewählt werden, der bereits ohne zusätzliche Message
88+
genau aussagt, was passiert ist.
89+
90+
## Nutze aussagekräftige Exception-Messages (GCG18006) <img src="/img/2.png" alt="recommendation level 2" />
91+
Die Exceptions-Message sollte den Grund für die Exception erklären und eindeutig
92+
beschreiben was getan werden muss, um die Exception zu vermeiden.
93+
94+
## Vermeide das Verbergen von Fehlern durch das Abfangen von generischen Exceptions (GCG18007) <img src="/img/1.png" alt="recommendation level 1" />
95+
Vermeide das Abfangen von nicht spezifischen Exceptions, wie z. B. `Exception`,
96+
`SystemException` etc. Nur auf der höchsten Ebene, als letzte Chance eine
97+
Exception abzufangen, sollten nicht spezifische Exceptions behandelt werden,
98+
um einen graceful Shutdown zu ermöglichen.
99+
100+
## Richtige Verwendung und Dokumentation von Exit-Codes (GCG18008) <img src="/img/1.png" alt="recommendation level 1" />
101+
Es sollte immer sichergestellt werden, dass beim Beenden der Applikation ein
102+
zutreffender Exit-Code ausgegeben wird. Dadurch ist es anderen Systemen möglich
103+
auf das Beenden der Anwendung zu reagieren, außerdem können Benutzer
104+
nachvollziehen wie und warum eine Anwendung beendet wurde.
105+
106+
Es hat sich etabliert, dass der Exit-Code `0` für ein erwartetes und
107+
erfolgreiches Beenden steht, hingegen alle abweichenden einen Fehler oder
108+
eine unerwartete Situation repräsentieren.
109+
110+
Alle Exit Codes müssen dokumentiert werden!
111+
112+
## Logging bei gezwungener Beendigung (GCG18009) <img src="/img/1.png" alt="recommendation level 1" />
113+
Wenn die Anwendung unerwartet beendet wird, sollte der Grund mit dem
114+
höchsten Log-Level dokumentiert werden.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Exceptions and Process Termination
2+
3+
## Don't use exceptions instead of `if...else` statements (GCG18001) <img src="/img/2.png" alt="recommendation level 2" />
4+
Exceptions shouldn't be used to make decisions, but to handle errors depending
5+
on their reason and impact.
6+
7+
This rule combines among others the rules `GCG18002` and `GCG18003`.
8+
9+
## Don't throw exceptions and catch them at the same time (GCG18002) <img src="/img/1.png" alt="recommendation level 1" />
10+
```
11+
// not okay!
12+
class Foo {
13+
public void AnyMethod() {
14+
try {
15+
throw new Exceptions();
16+
} catch {
17+
// do something
18+
}
19+
}
20+
}
21+
22+
// also not okay
23+
class Foo {
24+
public void AnyMethod() {
25+
try {
26+
MethodThrowingException();
27+
} catch {
28+
// do something
29+
}
30+
}
31+
32+
private void MethodThrowingException() {
33+
throw new Exception();
34+
}
35+
}
36+
37+
// maybe okay
38+
class Foo {
39+
public void AnyMethod() {
40+
try {
41+
MethodThrowingException();
42+
} catch {
43+
// do something
44+
}
45+
}
46+
47+
public void MethodThrowingException() {
48+
throw new Exception();
49+
}
50+
}
51+
52+
// okay!
53+
class Foo {
54+
public void AnyMethod() {
55+
try {
56+
new Bar().MethodThrowingException();
57+
} catch {
58+
// do something
59+
}
60+
}
61+
}
62+
63+
class Bar {
64+
public void MethodThrowingException() {
65+
throw new Exception();
66+
}
67+
}
68+
```
69+
70+
## Instead of catching null pointer exceptions, introduce null validations (GCG18003) <img src="/img/1.png" alt="recommendation level 1" />
71+
Null pointer exceptions arise when an object has an unexpected state. Instead of
72+
catching this kind of exceptions, you should validate the objects state.
73+
74+
## Throw exceptions rather than returning some kind of status value (GCG18004) <img src="/img/2.png" alt="recommendation level 2" />
75+
A code base that uses return values to report success or failure tends to have
76+
nested if-statements sprinkled all over the code. Quite often, a caller forgets
77+
to check the return value anyway.
78+
Structured exception has been introduced to allow you to throw exceptions and
79+
catch or replace them at a higher layer. In most systems it is quire common to
80+
throw exceptions whenever an unexpected situations occurs.
81+
82+
## Throw the most specific exception that is appropriate (GCG18005) <img src="/img/2.png" alt="recommendation level 2" />
83+
If it's possible to throw exceptions of a specific type, then you should that
84+
the most specific one, which don't need a message, to understand what happen.
85+
86+
## Provide a rich and meaningful exception message text (GCG18006) <img src="/img/2.png" alt="recommendation level 2" />
87+
The message should explain the cause of the exception, and clearly describe what
88+
needs to be done to avoid the exception.
89+
90+
## Don't swallow errors by catching generic exceptions (GCG18007) <img src="/img/1.png" alt="recommendation level 1" />
91+
Avoid swallowing errors by catching non-specific exceptions, such as
92+
`Exception`, `SystemException`, and so on, in application code. Only in
93+
top-level code, such as a last chance exception handler, you should catch a
94+
non-specific exception for logging purposes and a graceful shutdown of the
95+
application.
96+
97+
## Right usage and documentation of exit codes (GCG18008) <img src="/img/1.png" alt="recommendation level 1" />
98+
The application should always send a proper exit code, when the application
99+
was shutting down. So other applications can handle the termination of the
100+
process and users can understand why and how an application was stopped.
101+
102+
Exit code `0` means that the application was successfully stopped without any
103+
error. All other codes representing an unexpected situation.
104+
105+
All exit code must be documented!
106+
107+
## Logging in case of unexpected shutdown (GCG18009) <img src="/img/1.png" alt="recommendation level 1" />
108+
If the application stops unexpected, the reason should be logged in the
109+
highest possible log level.

coding-guidelines/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ nav:
2626
- Maintainability: guidelines/general/15000.md
2727
- Documentation: guidelines/general/16000.md
2828
- Logging: guidelines/general/17000.md
29+
- Exceptions and Process Termination: guidelines/general/18000.md
2930

3031
plugins:
3132
- search: { }
@@ -41,3 +42,4 @@ plugins:
4142
Tools and Automation: Tools und Automatisierung
4243
Namming Conventions: Namenskonvention
4344
Documentation: Dokumentation
45+
Exceptions and Process Termination: Exceptions und Prozessbeendigung

0 commit comments

Comments
 (0)