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:
parent
948243ef0c
commit
ce68ce1c65
@ -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");
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user