Files
oca8-lernen/src/org/eidecker/oca8lernen/general/FinallyTest.java

53 lines
1.0 KiB
Java

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