Notizen aus Lernsessions

This commit is contained in:
2019-10-26 23:02:28 +02:00
parent a4264d38ac
commit 0141e78355
6 changed files with 207 additions and 0 deletions

View 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;
}
}