diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/CheckedUncheckedExceptions.java b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/CheckedUncheckedExceptions.java new file mode 100644 index 0000000000..780189b654 --- /dev/null +++ b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/CheckedUncheckedExceptions.java @@ -0,0 +1,43 @@ +package com.baeldung.exceptions; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +public class CheckedUncheckedExceptions { + public static void checkedExceptionWithThrows() throws FileNotFoundException { + File file = new File("not_existing_file.txt"); + FileInputStream stream = new FileInputStream(file); + } + + public static void checkedExceptionWithTryCatch() { + File file = new File("not_existing_file.txt"); + try { + FileInputStream stream = new FileInputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public static int divideByZero() { + int numerator = 1; + int denominator = 0; + return numerator / denominator; + } + + public static void checkFile(String fileName) throws IncorrectFileNameException { + if (fileName == null || fileName.isEmpty()) { + throw new NullOrEmptyException("The filename is null."); + } + if (!isCorrectFileName(fileName)) { + throw new IncorrectFileNameException("Incorrect filename : " + fileName); + } + } + + private static boolean isCorrectFileName(String fileName) { + if (fileName.equals("wrongFileName.txt")) + return false; + else + return true; + } +} diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/IncorrectFileNameException.java b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/IncorrectFileNameException.java new file mode 100644 index 0000000000..9877fe25ca --- /dev/null +++ b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/IncorrectFileNameException.java @@ -0,0 +1,13 @@ +package com.baeldung.exceptions; + +public class IncorrectFileNameException extends Exception { + private static final long serialVersionUID = 1L; + + public IncorrectFileNameException(String errorMessage) { + super(errorMessage); + } + + public IncorrectFileNameException(String errorMessage, Throwable thr) { + super(errorMessage, thr); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/NullOrEmptyException.java b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/NullOrEmptyException.java new file mode 100644 index 0000000000..d4ee9c0d6f --- /dev/null +++ b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/NullOrEmptyException.java @@ -0,0 +1,14 @@ +package com.baeldung.exceptions; + +public class NullOrEmptyException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public NullOrEmptyException(String errorMessage) { + super(errorMessage); + } + + public NullOrEmptyException(String errorMessage, Throwable thr) { + super(errorMessage, thr); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/CheckedUncheckedExceptionsUnitTest.java b/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/CheckedUncheckedExceptionsUnitTest.java new file mode 100644 index 0000000000..05a01ba97d --- /dev/null +++ b/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/CheckedUncheckedExceptionsUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.exceptions; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.FileNotFoundException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Tests the {@link CheckedUncheckedExceptions}. + */ +public class CheckedUncheckedExceptionsUnitTest { + + @Test + public void whenFileNotExist_thenThrowException() { + assertThrows(FileNotFoundException.class, () -> { + CheckedUncheckedExceptions.checkedExceptionWithThrows(); + }); + } + + public void whenTryCatchExcetpion_thenSuccess() { + try { + CheckedUncheckedExceptions.checkedExceptionWithTryCatch(); + } catch (Exception e) { + Assertions.fail(e.getMessage()); + } + } + + @Test + public void whenDivideByZero_thenThrowException() { + assertThrows(ArithmeticException.class, () -> { + CheckedUncheckedExceptions.divideByZero(); + }); + } + + @Test + public void whenInvalidFile_thenThrowException() { + + assertThrows(IncorrectFileNameException.class, () -> { + CheckedUncheckedExceptions.checkFile("wrongFileName.txt"); + }); + } + + @Test + public void whenNullOrEmptyFile_thenThrowException() { + assertThrows(NullOrEmptyException.class, () -> { + CheckedUncheckedExceptions.checkFile(null); + }); + assertThrows(NullOrEmptyException.class, () -> { + CheckedUncheckedExceptions.checkFile(""); + }); + } +}