BAEL-2516 catching errors in catch blocks (#7075)

* BAEL-2516 catching errors in catch block

* BAEL-2516 catching errors in catch block; classes moved
This commit is contained in:
binary-joe 2019-06-03 06:42:11 +02:00 committed by Grzegorz Piwowarek
parent 948243ef0c
commit ce68ce1c65
3 changed files with 26 additions and 66 deletions

View File

@ -1,15 +0,0 @@
package com.baeldung.error;
public class ErrorGenerator {
public void throwException() throws Exception {
throw new Exception("checked");
}
public void throwRuntimeException() {
throw new RuntimeException("unchecked");
}
public void throwError() {
throw new Error("unchecked");
}
}

View File

@ -1,51 +0,0 @@
package com.baeldung.error;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ErrorGeneratorUnitTest {
private ErrorGenerator errorGenerator;
@Before
public void setUp() {
errorGenerator = new ErrorGenerator();
}
@Test
public void whenCheckedException_thenIsCaughtByCatchException() {
try {
errorGenerator.throwException();
} catch (Exception e) {
// caught! -> test pass
}
}
@Test
public void whenUncheckedException_thenIsCaughtByCatchException() {
try {
errorGenerator.throwRuntimeException();
} catch (Exception e) {
// caught! -> test pass
}
}
@Test(expected = Error.class)
public void whenError_thenIsNotCaughtByCatchException() {
try {
errorGenerator.throwError();
} catch (Exception e) {
Assert.fail(); // errors are not caught by catch exception
}
}
@Test
public void whenError_thenIsCaughtByCatchError() {
try {
errorGenerator.throwError();
} catch (Error e) {
// caught! -> test pass
}
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.error;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ErrorGeneratorUnitTest {
@Test(expected = AssertionError.class)
public void whenError_thenIsNotCaughtByCatchException() {
try {
throw new AssertionError();
} catch (Exception e) {
Assert.fail(); // errors are not caught by catch exception
}
}
@Test
public void whenError_thenIsCaughtByCatchError() {
try {
throw new AssertionError();
} catch (Error e) {
// caught! -> test pass
}
}
}