Throw and throws in Java (#4990)

This commit is contained in:
Kacper 2018-08-19 22:02:27 +02:00 committed by maibin
parent a21e940d8c
commit 9156ec3736
10 changed files with 198 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.throwsexception;
public class DataAccessException extends RuntimeException {
public DataAccessException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.throwsexception;
public class DivideByZeroException extends RuntimeException {
public DivideByZeroException(String message) {
super(message);
}
}

View File

@ -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();
}
}
}

View File

@ -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<String> findAll() throws SQLException {
throw new SQLException();
}
}

View File

@ -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();
}
}

View File

@ -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
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}