From 9156ec3736cf84639a118782295c22f1b656df7c Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 19 Aug 2018 22:02:27 +0200 Subject: [PATCH] Throw and throws in Java (#4990) --- .../baeldung/throwsexception/Calculator.java | 15 +++++++ .../throwsexception/DataAccessException.java | 9 ++++ .../DivideByZeroException.java | 9 ++++ .../com/baeldung/throwsexception/Main.java | 41 +++++++++++++++++++ .../throwsexception/PersonRepository.java | 18 ++++++++ .../throwsexception/SimpleService.java | 22 ++++++++++ .../baeldung/throwsexception/TryCatch.java | 13 ++++++ .../throwsexception/CalculatorUnitTest.java | 17 ++++++++ .../PersonRepositoryUnitTest.java | 32 +++++++++++++++ .../SimpleServiceUnitTest.java | 22 ++++++++++ 10 files changed, 198 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/Calculator.java create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/DataAccessException.java create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/DivideByZeroException.java create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/Main.java create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/PersonRepository.java create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/SimpleService.java create mode 100644 core-java/src/main/java/com/baeldung/throwsexception/TryCatch.java create mode 100644 core-java/src/test/java/com/baeldung/throwsexception/CalculatorUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/throwsexception/SimpleServiceUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/throwsexception/Calculator.java b/core-java/src/main/java/com/baeldung/throwsexception/Calculator.java new file mode 100644 index 0000000000..50dbc9c774 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/Calculator.java @@ -0,0 +1,15 @@ +package com.baeldung.throwsexception; + +public class Calculator { + + public double divide(double a, double b) { + if (b == 0) { + throw new DivideByZeroException("Divider cannot be equal to zero!"); + } + return a/b; + } + +} + + + diff --git a/core-java/src/main/java/com/baeldung/throwsexception/DataAccessException.java b/core-java/src/main/java/com/baeldung/throwsexception/DataAccessException.java new file mode 100644 index 0000000000..0b371dcedb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/DataAccessException.java @@ -0,0 +1,9 @@ +package com.baeldung.throwsexception; + +public class DataAccessException extends RuntimeException { + + public DataAccessException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/core-java/src/main/java/com/baeldung/throwsexception/DivideByZeroException.java b/core-java/src/main/java/com/baeldung/throwsexception/DivideByZeroException.java new file mode 100644 index 0000000000..4413374c99 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/DivideByZeroException.java @@ -0,0 +1,9 @@ +package com.baeldung.throwsexception; + +public class DivideByZeroException extends RuntimeException { + + public DivideByZeroException(String message) { + super(message); + } + +} diff --git a/core-java/src/main/java/com/baeldung/throwsexception/Main.java b/core-java/src/main/java/com/baeldung/throwsexception/Main.java new file mode 100644 index 0000000000..17fbf5a582 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/Main.java @@ -0,0 +1,41 @@ +package com.baeldung.throwsexception; + +import com.sun.mail.iap.ConnectionException; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.net.SocketException; + +public class Main { + + public static void main(String[] args) throws FileNotFoundException { + TryCatch tryCatch = new TryCatch(); + + try { + tryCatch.execute(); + } catch (ConnectionException | SocketException ex) { + System.out.println("IOException"); + } catch (Exception ex) { + System.out.println("General exception"); + } + + checkedException(); + checkedExceptionWithThrows(); + } + + private static void checkedExceptionWithThrows() throws FileNotFoundException { + File file = new File("not_existing_file.txt"); + FileInputStream stream = new FileInputStream(file); + } + + private static void checkedException() { + File file = new File("not_existing_file.txt"); + try { + FileInputStream stream = new FileInputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + +} diff --git a/core-java/src/main/java/com/baeldung/throwsexception/PersonRepository.java b/core-java/src/main/java/com/baeldung/throwsexception/PersonRepository.java new file mode 100644 index 0000000000..5fcbeb3d5f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/PersonRepository.java @@ -0,0 +1,18 @@ +package com.baeldung.throwsexception; + +import javax.annotation.Nullable; +import java.sql.SQLException; +import java.util.List; + +public class PersonRepository { + + @Nullable + public String findNameById(String id) { + return id == null ? null : "example-name"; + } + + public List findAll() throws SQLException { + throw new SQLException(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/throwsexception/SimpleService.java b/core-java/src/main/java/com/baeldung/throwsexception/SimpleService.java new file mode 100644 index 0000000000..6bb8b90bf1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/SimpleService.java @@ -0,0 +1,22 @@ +package com.baeldung.throwsexception; + +import java.sql.SQLException; + +public class SimpleService { + + private PersonRepository personRepository = new PersonRepository(); + + public void wrappingException() { + try { + personRepository.findAll(); + } catch (SQLException e) { + throw new DataAccessException("SQL Exception", e); + } + } + + public void runtimeNullPointerException() { + String a = null; + a.length(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/throwsexception/TryCatch.java b/core-java/src/main/java/com/baeldung/throwsexception/TryCatch.java new file mode 100644 index 0000000000..2fd87f124d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/throwsexception/TryCatch.java @@ -0,0 +1,13 @@ +package com.baeldung.throwsexception; + +import com.sun.mail.iap.ConnectionException; + +import java.net.SocketException; + +public class TryCatch { + + public void execute() throws SocketException, ConnectionException, Exception { + //code that would throw any of: SocketException, ConnectionException, Exception + } + +} diff --git a/core-java/src/test/java/com/baeldung/throwsexception/CalculatorUnitTest.java b/core-java/src/test/java/com/baeldung/throwsexception/CalculatorUnitTest.java new file mode 100644 index 0000000000..ef838ed304 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/throwsexception/CalculatorUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.throwsexception; + +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class CalculatorUnitTest { + + @Test + public void whenDividerIsZero_thenDivideByZeroExceptionIsThrown() { + Calculator calculator = new Calculator(); + + assertThrows(DivideByZeroException.class, + () -> calculator.divide(10, 0)); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java b/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java new file mode 100644 index 0000000000..6294d40090 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.throwsexception; + +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PersonRepositoryUnitTest { + + PersonRepository personRepository = new PersonRepository(); + + @Test + public void whenIdIsNull_thenExceptionIsThrown() throws Exception { + assertThrows(Exception.class, + () -> + Optional + .ofNullable(personRepository.findNameById(null)) + .orElseThrow(Exception::new)); + } + + @Test + public void whenIdIsNonNull_thenNoExceptionIsThrown() throws Exception { + assertAll( + () -> + Optional + .ofNullable(personRepository.findNameById("id")) + .orElseThrow(Exception::new)); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/throwsexception/SimpleServiceUnitTest.java b/core-java/src/test/java/com/baeldung/throwsexception/SimpleServiceUnitTest.java new file mode 100644 index 0000000000..b9a658a960 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/throwsexception/SimpleServiceUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.throwsexception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class SimpleServiceUnitTest { + + SimpleService simpleService = new SimpleService(); + + @Test + void whenSQLExceptionIsThrown_thenShouldBeRethrownWithWrappedException() { + assertThrows(DataAccessException.class, + () -> simpleService.wrappingException()); + } + + @Test + void whenCalled_thenNullPointerExceptionIsThrown() { + assertThrows(NullPointerException.class, + () -> simpleService.runtimeNullPointerException()); + } +} \ No newline at end of file