Skip to content

Commit be50978

Browse files
crivasgomezFridaTveit
authored andcommitted
Add new exercise Error Handling (exercism#1338)
* Add new exercise Error Handling * Add requested changes * Undo unnecessary change to ErrorHandlingTest.java * Add requested changes * Add requested changes * Check exception message * Add requested changes to ErrorHandlingTest.java * Add hints.md * Add requested suggestions * Expand hints.md * Add gramatical suggestions
1 parent fda9d39 commit be50978

12 files changed

Lines changed: 361 additions & 0 deletions

‎config.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,17 @@
395395
"unlocked_by": null,
396396
"uuid": "8dfc2f0d-1141-46e9-95e2-6f35ccf6f160"
397397
},
398+
{
399+
"core": false,
400+
"difficulty": 4,
401+
"slug": "error-handling",
402+
"topics": [
403+
"exception_handling",
404+
"optional_values"
405+
],
406+
"unlocked_by": "triangle",
407+
"uuid": "846ae792-7ca7-43e1-b523-bb1ec9fa08eb"
408+
},
398409
{
399410
"core": false,
400411
"difficulty": 4,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
This exercise requires you to handle exceptions. An [exception](https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html) is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
2+
3+
In Java, there are two types of exceptions: checked and unchecked exceptions.
4+
5+
- [Checked vs Unchecked Exceptions in Java](https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/)
6+
7+
- [Unchecked Exceptions — The Controversy](https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html)
8+
9+
## Checked exceptions
10+
11+
Checked exceptions are the exceptions that are checked at [compile time](https://en.wikipedia.org/wiki/Compile_time).
12+
13+
### Practical implications
14+
15+
You have to declare them in the [method signature](https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html) of any method that can [throw](https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html) a checked exception and handle or rethrow them when calling any method that can throw a checked exception.
16+
17+
This is because checked exceptions are meant to be handled at runtime, i.e. they are errors you can recover from.
18+
19+
### Examples of where they are used
20+
21+
They're often used when a method can't return any valid result, for example a search method which hasn't found the item it was searching for.
22+
23+
It's an alternative to returning null or a error code. A checked exception is better than those alternatives because it forces the user of the method to consider the error case.
24+
25+
## Unchecked exceptions
26+
27+
Unchecked exceptions are the exceptions that are not checked at [compile time](https://en.wikipedia.org/wiki/Compile_time).
28+
29+
### Practical implications
30+
31+
You don't have to declare them in the [method signature](https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html) of methods that can [throw](https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html) an unchecked exception and handle or rethrow them when calling any method that can throw an unchecked exception.
32+
33+
### Examples of where they are used
34+
35+
Unchecked exceptions are mean to be used for any error than can't be handled at runtime, e.g. running out of memory.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CustomCheckedException extends Exception {
2+
3+
CustomCheckedException() {
4+
super();
5+
}
6+
7+
CustomCheckedException(String message) {
8+
super(message);
9+
}
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CustomUncheckedException extends RuntimeException {
2+
3+
CustomUncheckedException() {
4+
super();
5+
}
6+
7+
CustomUncheckedException(String message) {
8+
super(message);
9+
}
10+
11+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.io.IOException;
2+
import java.util.Optional;
3+
4+
class ErrorHandling {
5+
6+
void handleErrorByThrowingIllegalArgumentException() {
7+
throw new IllegalArgumentException();
8+
}
9+
10+
void handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(String message) {
11+
throw new IllegalArgumentException(message);
12+
}
13+
14+
void handleErrorByThrowingAnyCheckedException() throws IOException {
15+
throw new IOException();
16+
}
17+
18+
void handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(String message) throws IOException {
19+
throw new IOException(message);
20+
}
21+
22+
void handleErrorByThrowingAnyUncheckedException() {
23+
throw new NullPointerException();
24+
}
25+
26+
void handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(String message) {
27+
throw new NullPointerException(message);
28+
}
29+
30+
void handleErrorByThrowingCustomCheckedException() throws CustomCheckedException {
31+
throw new CustomCheckedException();
32+
}
33+
34+
void handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(String message) throws CustomCheckedException {
35+
throw new CustomCheckedException(message);
36+
}
37+
38+
void handleErrorByThrowingCustomUncheckedException() {
39+
throw new CustomUncheckedException();
40+
}
41+
42+
void handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(String message) {
43+
throw new CustomUncheckedException(message);
44+
}
45+
46+
Optional<Integer> handleErrorByReturningOptionalInstance(String integer) {
47+
if (tryParseInt(integer)) {
48+
return Optional.of(Integer.parseInt(integer));
49+
} else {
50+
return Optional.empty();
51+
}
52+
}
53+
54+
private boolean tryParseInt(String integer) {
55+
try {
56+
Integer.parseInt(integer);
57+
return true;
58+
} catch (NumberFormatException e) {
59+
return false;
60+
}
61+
}
62+
63+
}

‎exercises/error-handling/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Error Handling
2+
3+
Implement various kinds of error handling and resource management.
4+
5+
An important point of programming is how to handle errors and close
6+
resources even if errors occur.
7+
8+
This exercise requires you to handle various errors. Because error handling
9+
is rather programming language specific you'll have to refer to the tests
10+
for your track to see what's exactly required.
11+
12+
# Running the tests
13+
14+
You can run all the tests for an exercise by entering
15+
16+
```sh
17+
$ gradle test
18+
```
19+
20+
in your terminal.
21+
22+
## Submitting Incomplete Solutions
23+
24+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CustomCheckedException extends Exception {
2+
3+
CustomCheckedException() {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
CustomCheckedException(String message) {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CustomUncheckedException extends RuntimeException {
2+
3+
CustomUncheckedException() {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
CustomUncheckedException(String message) {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Optional;
2+
3+
class ErrorHandling {
4+
5+
void handleErrorByThrowingIllegalArgumentException() {
6+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7+
}
8+
9+
void handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(String message) {
10+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
11+
}
12+
13+
void handleErrorByThrowingAnyCheckedException() {
14+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
15+
}
16+
17+
void handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(String message) {
18+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
19+
}
20+
21+
void handleErrorByThrowingAnyUncheckedException() {
22+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
23+
}
24+
25+
void handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(String message) {
26+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
27+
}
28+
29+
void handleErrorByThrowingCustomCheckedException() {
30+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
31+
}
32+
33+
void handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(String message) {
34+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
35+
}
36+
37+
void handleErrorByThrowingCustomUncheckedException() {
38+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
39+
}
40+
41+
void handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(String message) {
42+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
43+
}
44+
45+
Optional<Integer> handleErrorByReturningOptionalInstance(String integer) {
46+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
47+
}
48+
49+
}

0 commit comments

Comments
 (0)