BAEL-2516 catching errors in catch block (#6761)
This commit is contained in:
parent
93dfb77f1f
commit
d373336b74
|
@ -0,0 +1,15 @@
|
|||
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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue