Skip to content

Commit 9156ec3

Browse files
kacperkozamaibin
authored andcommitted
Throw and throws in Java (eugenp#4990)
1 parent a21e940 commit 9156ec3

10 files changed

Lines changed: 198 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.throwsexception;
2+
3+
public class Calculator {
4+
5+
public double divide(double a, double b) {
6+
if (b == 0) {
7+
throw new DivideByZeroException("Divider cannot be equal to zero!");
8+
}
9+
return a/b;
10+
}
11+
12+
}
13+
14+
15+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.throwsexception;
2+
3+
public class DataAccessException extends RuntimeException {
4+
5+
public DataAccessException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.throwsexception;
2+
3+
public class DivideByZeroException extends RuntimeException {
4+
5+
public DivideByZeroException(String message) {
6+
super(message);
7+
}
8+
9+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.throwsexception;
2+
3+
import com.sun.mail.iap.ConnectionException;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
8+
import java.net.SocketException;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) throws FileNotFoundException {
13+
TryCatch tryCatch = new TryCatch();
14+
15+
try {
16+
tryCatch.execute();
17+
} catch (ConnectionException | SocketException ex) {
18+
System.out.println("IOException");
19+
} catch (Exception ex) {
20+
System.out.println("General exception");
21+
}
22+
23+
checkedException();
24+
checkedExceptionWithThrows();
25+
}
26+
27+
private static void checkedExceptionWithThrows() throws FileNotFoundException {
28+
File file = new File("not_existing_file.txt");
29+
FileInputStream stream = new FileInputStream(file);
30+
}
31+
32+
private static void checkedException() {
33+
File file = new File("not_existing_file.txt");
34+
try {
35+
FileInputStream stream = new FileInputStream(file);
36+
} catch (FileNotFoundException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.throwsexception;
2+
3+
import javax.annotation.Nullable;
4+
import java.sql.SQLException;
5+
import java.util.List;
6+
7+
public class PersonRepository {
8+
9+
@Nullable
10+
public String findNameById(String id) {
11+
return id == null ? null : "example-name";
12+
}
13+
14+
public List<String> findAll() throws SQLException {
15+
throw new SQLException();
16+
}
17+
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.throwsexception;
2+
3+
import java.sql.SQLException;
4+
5+
public class SimpleService {
6+
7+
private PersonRepository personRepository = new PersonRepository();
8+
9+
public void wrappingException() {
10+
try {
11+
personRepository.findAll();
12+
} catch (SQLException e) {
13+
throw new DataAccessException("SQL Exception", e);
14+
}
15+
}
16+
17+
public void runtimeNullPointerException() {
18+
String a = null;
19+
a.length();
20+
}
21+
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.throwsexception;
2+
3+
import com.sun.mail.iap.ConnectionException;
4+
5+
import java.net.SocketException;
6+
7+
public class TryCatch {
8+
9+
public void execute() throws SocketException, ConnectionException, Exception {
10+
//code that would throw any of: SocketException, ConnectionException, Exception
11+
}
12+
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.throwsexception;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
public class CalculatorUnitTest {
8+
9+
@Test
10+
public void whenDividerIsZero_thenDivideByZeroExceptionIsThrown() {
11+
Calculator calculator = new Calculator();
12+
13+
assertThrows(DivideByZeroException.class,
14+
() -> calculator.divide(10, 0));
15+
}
16+
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.throwsexception;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Optional;
6+
7+
import static org.junit.jupiter.api.Assertions.assertAll;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
public class PersonRepositoryUnitTest {
11+
12+
PersonRepository personRepository = new PersonRepository();
13+
14+
@Test
15+
public void whenIdIsNull_thenExceptionIsThrown() throws Exception {
16+
assertThrows(Exception.class,
17+
() ->
18+
Optional
19+
.ofNullable(personRepository.findNameById(null))
20+
.orElseThrow(Exception::new));
21+
}
22+
23+
@Test
24+
public void whenIdIsNonNull_thenNoExceptionIsThrown() throws Exception {
25+
assertAll(
26+
() ->
27+
Optional
28+
.ofNullable(personRepository.findNameById("id"))
29+
.orElseThrow(Exception::new));
30+
}
31+
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.throwsexception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class SimpleServiceUnitTest {
8+
9+
SimpleService simpleService = new SimpleService();
10+
11+
@Test
12+
void whenSQLExceptionIsThrown_thenShouldBeRethrownWithWrappedException() {
13+
assertThrows(DataAccessException.class,
14+
() -> simpleService.wrappingException());
15+
}
16+
17+
@Test
18+
void whenCalled_thenNullPointerExceptionIsThrown() {
19+
assertThrows(NullPointerException.class,
20+
() -> simpleService.runtimeNullPointerException());
21+
}
22+
}

0 commit comments

Comments
 (0)