[JAVA-12620] Split core-java-exceptions module

This commit is contained in:
Haroon Khan
2022-06-14 21:56:25 +01:00
parent e31a4dff8d
commit 18bcb72f85
8 changed files with 30 additions and 18 deletions

View File

@@ -1,5 +1,10 @@
### Relevant Articles:
## Core Java Exceptions
This module contains articles about core java exceptions
### Relevant articles:
- [Java ArrayIndexOutOfBoundsException](https://www.baeldung.com/java-arrayindexoutofboundsexception)
- [Java Missing Return Statement](https://www.baeldung.com/java-missing-return-statement)
- [Convert long to int Type in Java](https://www.baeldung.com/java-convert-long-to-int)
- [“Sneaky Throws” in Java](https://www.baeldung.com/java-sneaky-throws)
- [[<-- Prev]](../core-java-exceptions-3)

View File

@@ -22,6 +22,12 @@
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,22 @@
package com.baeldung.exception.sneakythrows;
import lombok.SneakyThrows;
import java.io.IOException;
public class SneakyThrowsExamples {
public static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
throw (E) e;
}
public static void throwSneakyIOException() {
sneakyThrow(new IOException("sneaky"));
}
@SneakyThrows
public static void throwSneakyIOExceptionUsingLombok() {
throw new IOException("lombok sneaky");
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.exception.sneakythrows;
import org.junit.Test;
import java.io.IOException;
import static com.baeldung.exception.sneakythrows.SneakyThrowsExamples.throwSneakyIOException;
import static com.baeldung.exception.sneakythrows.SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class SneakyThrowsExamplesUnitTest {
@Test
public void throwSneakyIOException_IOExceptionShouldBeThrown() {
assertThatThrownBy(() -> throwSneakyIOException())
.isInstanceOf(IOException.class)
.hasMessage("sneaky")
.hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOException");
}
@Test
public void throwSneakyIOExceptionUsingLombok_IOExceptionShouldBeThrown() {
assertThatThrownBy(() -> throwSneakyIOExceptionUsingLombok())
.isInstanceOf(IOException.class)
.hasMessage("lombok sneaky")
.hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok");
}
}