Skip to content

Commit 550f65b

Browse files
committed
refactor!: reorganize project into separate modules for independent testing
- Convert single-module project to multi-module Maven structure - Create exception-handling module for practical exception handling exercises - Create custom-exceptions module for custom exception creation tasks - Move existing tests and source files to appropriate modules - Update README with module-specific instructions and testing commands - Update parent pom.xml to use pom packaging with module declarations BREAKING CHANGE: Project structure changed from single module to multi-module, requiring updated build and test commands
1 parent e730177 commit 550f65b

File tree

9 files changed

+373
-53
lines changed

9 files changed

+373
-53
lines changed

README.md

Lines changed: 109 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,146 @@
55
[![Maven Dependency Manager](https://img.shields.io/badge/dependency%20manager-Maven-AA215A.svg?style=for-the-badge)][3]
66

77
The goal of this exercise is to practise:
8-
- Creating custom exceptions
98
- Handling exceptions
9+
- Creating custom exceptions
1010

11-
For the exercises below, we've provided the starter project above.
11+
This project is organized into two modules that can be compiled and tested independently:
1212

13-
## :pushpin: Exceptions
13+
- **exception-handling**: Practical exception handling exercises
14+
- **custom-exceptions**: Custom exception creation and file processing exercises
1415

15-
### Question 1
16+
### Running Tests
1617

17-
Is the following code legal?
18+
To run all tests across both modules:
1819

19-
```java
20-
try {
20+
```shell
21+
./mvnw clean test
22+
```
2123

22-
} finally {
24+
To test a specific module:
2325

24-
}
26+
```shell
27+
./mvnw clean test -pl exception-handling
28+
./mvnw clean test -pl custom-exceptions
2529
```
2630

27-
### Question 2
31+
## :pushpin: Exception Handling
32+
33+
Create a class called `ExceptionExercises` in the `exception-handling/src/main/java/com/cbfacademy` directory with the following methods that demonstrate proper exception handling techniques. Each method should handle specific exceptions gracefully and return appropriate values.
34+
35+
Using the [Java Exception Handling documentation][4] as a guide, implement the methods described below. In each method, replace `throw new RuntimeException("Not implemented")` with your code.
36+
37+
### Safe Division
38+
39+
Create a method that performs division while handling potential arithmetic exceptions:
2840

29-
What exception types can be caught by the following handler?
3041
```java
31-
catch (Exception e) {
32-
42+
public Double divide(Double numerator, Double denominator) {
43+
// TODO: Implement this method to divide the numerator by the denominator
44+
// - return the result of the division
45+
// - if the denominator is 0, catch the ArithmeticException and return null
46+
// - if either parameter is null, return null
47+
throw new RuntimeException("Not implemented");
3348
}
3449
```
3550

36-
What is wrong with using this type of exception handler?
51+
### Safe List Access
3752

38-
### Question 3
53+
Create a method that safely accesses list elements:
3954

40-
Is there anything wrong with the following exception handler as written? Will this code compile?
55+
```java
56+
public Integer getListElement(List<Integer> list, int index) {
57+
// TODO: Implement this method to return the element at the specified index
58+
// - return the element at the given index
59+
// - if the index is out of bounds, catch IndexOutOfBoundsException and return -1
60+
// - if the list is null, return -1
61+
throw new RuntimeException("Not implemented");
62+
}
63+
```
64+
65+
### Safe String to Integer Conversion
66+
67+
Create a method that safely converts strings to integers:
4168

4269
```java
43-
try {
70+
public Integer convertToInteger(String numberString) {
71+
// TODO: Implement this method to convert a string to an integer
72+
// - return the integer value of the string
73+
// - if the string cannot be converted, catch NumberFormatException and return 0
74+
// - if the string is null, return null
75+
throw new RuntimeException("Not implemented");
76+
}
77+
```
4478

45-
} catch (Exception e) {
79+
### Safe String Processing
4680

47-
} catch (ArithmeticException a) {
81+
Create a method that safely processes strings:
4882

83+
```java
84+
public String getStringLength(String input) {
85+
// TODO: Implement this method to return a formatted string with the length
86+
// - return "Length: [length]" where [length] is the length of the input string
87+
// - if the input is null, catch NullPointerException and return "Length: 0"
88+
throw new RuntimeException("Not implemented");
4989
}
5090
```
5191

52-
### Question 4
92+
### Multiple Exception Handling
93+
94+
Create a method that demonstrates handling multiple types of exceptions:
5395

5496
```java
55-
int[] A;
56-
A[0] = 0;
97+
public String processData(List<String> data, int index) {
98+
// TODO: Implement this method to process list data
99+
// - get the string at the specified index from the list
100+
// - convert that string to an integer
101+
// - return "Processed: [integer_value]"
102+
// - if index is out of bounds, return "Index out of bounds"
103+
// - if string conversion fails, return "Invalid number format"
104+
// - if the list is null, return "List is null"
105+
throw new RuntimeException("Not implemented");
106+
}
107+
```
108+
109+
**Example Usage:**
110+
```java
111+
ExceptionExercises exercises = new ExceptionExercises();
112+
113+
// Safe division
114+
Double result1 = exercises.divide(10.0, 2.0); // Returns 5.0
115+
Double result2 = exercises.divide(10.0, 0.0); // Returns null (handles ArithmeticException)
116+
117+
// Safe list access
118+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
119+
Integer element1 = exercises.getListElement(numbers, 2); // Returns 3
120+
Integer element2 = exercises.getListElement(numbers, 10); // Returns -1 (handles IndexOutOfBoundsException)
121+
122+
// Safe string conversion
123+
Integer num1 = exercises.convertToInteger("123"); // Returns 123
124+
Integer num2 = exercises.convertToInteger("abc"); // Returns 0 (handles NumberFormatException)
57125
```
58126

59-
The above code produces (choose 1):
127+
#### :white_check_mark: Verify Your Implementation
60128

61-
- [ ] an error
62-
- [ ] a checked exception
63-
- [ ] an unchecked exception
64-
- [ ] a compile error
65-
- [ ] no exception
129+
To verify that your code works as expected, run the provided unit tests.
66130

67-
### Question 5
131+
In your terminal, ensure that you are in the root directory of this repo, then run the following command:
68132

69-
The JVM starts running your program, but the JVM can't find the Java platform classes.
70-
(The Java platform classes reside in classes.zip or rt.jar.)
133+
```shell
134+
./mvnw clean test -pl exception-handling
135+
```
71136

72-
What happens (choose 1):
137+
Or to run only the ExceptionExercises tests:
73138

74-
- [ ] an error
75-
- [ ] a checked exception
76-
- [ ] an unchecked exception
77-
- [ ] a compile error
78-
- [ ] no exception
139+
```shell
140+
./mvnw clean test -pl exception-handling -Dtest=ExceptionExercisesTest
141+
```
79142

80143
## :pushpin: Custom Exceptions
81144

82-
Create a custom (checked) exception class called `FilenameException`.
145+
Create a custom (checked) exception class called `FilenameException` in the `custom-exceptions/src/main/java/com/cbfacademy` directory.
83146

84-
Create a class called `FileExtension` with the following methods:
147+
Create a class called `FileExtension` in the same directory with the following methods:
85148

86149
- `boolean check(String filename)`
87150
- `Map<String, int> map(List<String> filenames)`
@@ -110,16 +173,16 @@ For the following list of file names: `Arrays.asList("App.java", "App.txt", null
110173

111174
To verify that your code works as expected, run the provided unit tests.
112175

113-
In your terminal, ensure that you are in the `java-exceptions` folder, then run the following command:
176+
In your terminal, ensure that you are in the root directory of this repo, then run the following command:
114177

115178
```shell
116-
./mvnw clean test
179+
./mvnw clean test -pl custom-exceptions
117180
```
118181

119-
If you are on Windows, run this command instead:
182+
Or to run only the FileExtension tests:
120183

121-
```bat
122-
mvnw clean test
184+
```shell
185+
./mvnw clean test -pl custom-exceptions -Dtest=FileExtensionTest
123186
```
124187

125188
Your implementation is correct when all tests pass.
@@ -140,4 +203,5 @@ mvnw -q clean compile exec:java
140203

141204
[1]: https://docs.oracle.com/javase/21/docs/api/index.html
142205
[2]: https://junit.org/junit5/
143-
[3]: https://maven.apache.org/
206+
[3]: https://maven.apache.org/
207+
[4]: https://docs.oracle.com/javase/tutorial/essential/exceptions/

custom-exceptions/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>java-exceptions</artifactId>
6+
<groupId>com.cbfacademy</groupId>
7+
<version>1.0.0</version>
8+
</parent>
9+
10+
<groupId>com.cbfacademy</groupId>
11+
<artifactId>custom-exceptions</artifactId>
12+
<version>1.0.0</version>
13+
14+
<name>custom-exceptions</name>
15+
16+
<build>
17+
<pluginManagement>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<configuration>
23+
<testExcludes>
24+
<!-- <testExclude>**/FileExtensionTest.java</testExclude> -->
25+
<!-- <testExclude>**/FilenameExceptionTest.java</testExclude> -->
26+
</testExcludes>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</pluginManagement>
31+
</build>
32+
</project>

exception-handling/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>java-exceptions</artifactId>
6+
<groupId>com.cbfacademy</groupId>
7+
<version>1.0.0</version>
8+
</parent>
9+
10+
<groupId>com.cbfacademy</groupId>
11+
<artifactId>exception-handling</artifactId>
12+
<version>1.0.0</version>
13+
14+
<name>exception-handling</name>
15+
16+
<build>
17+
<pluginManagement>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<configuration>
23+
<testExcludes>
24+
<!-- <testExclude>**/ExceptionExercisesTest.java</testExclude> -->
25+
</testExcludes>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</pluginManagement>
30+
</build>
31+
</project>

0 commit comments

Comments
 (0)