Notizen aus Lernsessions
This commit is contained in:
52
src/org/eidecker/oca8lernen/general/FinallyTest.java
Normal file
52
src/org/eidecker/oca8lernen/general/FinallyTest.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package org.eidecker.oca8lernen.general;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class FinallyTest {
|
||||
|
||||
@Test
|
||||
public void testFinally() {
|
||||
int a = divZero(5);
|
||||
assertEquals(5, a);
|
||||
}
|
||||
|
||||
|
||||
private int divZero(int a) {
|
||||
try {
|
||||
return a/0;
|
||||
} catch (ArithmeticException e) {
|
||||
return sideEffect();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception vom Typ Exception gefangen");
|
||||
} finally {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionBloecke() {
|
||||
|
||||
assertThrows(Exception.class, () -> {
|
||||
try {
|
||||
int a = 1/0;
|
||||
} catch (ArithmeticException e) {
|
||||
System.out.println("ArithmeticException gefangen");
|
||||
throw new Exception();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception vom Typ Exception gefangen");
|
||||
} finally {
|
||||
System.out.println("Finally");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private int sideEffect() {
|
||||
System.out.println("Nur ein Seiteneffekt");
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user