|
1 | | -# Java Exercise Template |
2 | | -This template repository is used to create autograded Java exercise repositories for CBF Academy bootcamps. It includes a GitHub Classroom autograding workflow that scores student submissions on functionality and code quality by executing unit tests and submitting the code changes for review by an automated agent. |
3 | | - |
4 | | -## Usage |
5 | | - |
6 | | -1. Create a new repository for the exercise, with the following settings: |
7 | | - - Template: Select this repository |
8 | | - - Name: Use the `java-exercises-[exercise name]` naming convention, e.g. `java-exercises-spring-boot` |
9 | | - - Visibility: Private |
10 | | -2. After initialising, navigate to the repo settings and set as a template, so it can be used for assignments |
11 | | -3. Commit README, Maven assets, starter code and unit tests to the main branch |
12 | | -4. Create a solutions branch and commit any reference solutions code to it |
13 | | -5. Modify the `MAX_TESTS` environment variable to reflect the total number of tests available |
14 | | -6. Push all changes |
15 | | - |
16 | | -## Testing |
17 | | - |
18 | | -1. Create a new Classroom assignment using the exercise repo as the starter template, with the following settings |
19 | | - - Repository visibility: Private |
20 | | - - Grant students admin access to their repository: Disabled |
21 | | - - Copy the default branch only: Enabled |
22 | | - - Supported editor: Don't use an online IDE |
23 | | - - Protected file paths: `.github/**/*`, `**/test/**/*` |
24 | | - - Enable feedback pull requests: Enabled |
25 | | -2. Accept the assignment from a test account. |
26 | | -3. Commit and push |
27 | | -4. Review the Actions output and Feedback PR comment to ensure everything operates as expected |
| 1 | +# Exception Handling |
| 2 | + |
| 3 | +[][1] |
| 4 | +[][2] |
| 5 | +[][3] |
| 6 | + |
| 7 | +The goal of this exercise is to practise: |
| 8 | +- Creating custom exceptions |
| 9 | +- Handling exceptions |
| 10 | + |
| 11 | +For the exercises below, we've provided the starter project above. |
| 12 | + |
| 13 | +## :pushpin: Exceptions |
| 14 | + |
| 15 | +### Question 1 |
| 16 | + |
| 17 | +Is the following code legal? |
| 18 | + |
| 19 | +```java |
| 20 | +try { |
| 21 | + |
| 22 | +} finally { |
| 23 | + |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +### Question 2 |
| 28 | + |
| 29 | +What exception types can be caught by the following handler? |
| 30 | +```java |
| 31 | +catch (Exception e) { |
| 32 | + |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +What is wrong with using this type of exception handler? |
| 37 | + |
| 38 | +### Question 3 |
| 39 | + |
| 40 | +Is there anything wrong with the following exception handler as written? Will this code compile? |
| 41 | + |
| 42 | +```java |
| 43 | +try { |
| 44 | + |
| 45 | +} catch (Exception e) { |
| 46 | + |
| 47 | +} catch (ArithmeticException a) { |
| 48 | + |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Question 4 |
| 53 | + |
| 54 | +```java |
| 55 | +int[] A; |
| 56 | +A[0] = 0; |
| 57 | +``` |
| 58 | + |
| 59 | +The above code produces (choose 1): |
| 60 | + |
| 61 | +- [ ] an error |
| 62 | +- [ ] a checked exception |
| 63 | +- [ ] an unchecked exception |
| 64 | +- [ ] a compile error |
| 65 | +- [ ] no exception |
| 66 | + |
| 67 | +### Question 5 |
| 68 | + |
| 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.) |
| 71 | + |
| 72 | +What happens (choose 1): |
| 73 | + |
| 74 | +- [ ] an error |
| 75 | +- [ ] a checked exception |
| 76 | +- [ ] an unchecked exception |
| 77 | +- [ ] a compile error |
| 78 | +- [ ] no exception |
| 79 | + |
| 80 | +## :pushpin: Custom Exceptions |
| 81 | + |
| 82 | +Create a custom (checked) exception class called `FilenameException`. |
| 83 | + |
| 84 | +Create a class called `FileExtension` with the following methods: |
| 85 | + |
| 86 | +- `boolean check(String filename)` |
| 87 | +- `Map<String, int> map(List<String> filenames)` |
| 88 | + |
| 89 | +The `check` method should: |
| 90 | +- return `true` when the file extension is `.java` |
| 91 | +- return `false` when the file extension is not `.java` |
| 92 | +- throw a `FilenameException` when the file name is `null` or an empty string. |
| 93 | + |
| 94 | +The `map` method should: |
| 95 | +- check each provided file's extension and map the returned value as 1 if true or 0 if false |
| 96 | +- map `-1` when an exception occurs |
| 97 | + |
| 98 | +**Example** |
| 99 | + |
| 100 | +For the following list of file names: `Arrays.asList("App.java", "App.txt", null, "App.md")`, the `map` method should return a map with the following entries: |
| 101 | + |
| 102 | +```txt |
| 103 | +{"App.java", 1}, |
| 104 | +{"App.txt", 0}, |
| 105 | +{null, -1}, |
| 106 | +{"App.md", 0} |
| 107 | +``` |
| 108 | + |
| 109 | +#### :white_check_mark: Verify Your Implementation |
| 110 | + |
| 111 | +To verify that your code works as expected, run the provided unit tests. |
| 112 | + |
| 113 | +In your terminal, ensure that you are in the `java-exceptions` folder, then run the following command: |
| 114 | + |
| 115 | +```shell |
| 116 | +./mvnw clean test |
| 117 | +``` |
| 118 | + |
| 119 | +If you are on Windows, run this command instead: |
| 120 | + |
| 121 | +```bat |
| 122 | +mvnw clean test |
| 123 | +``` |
| 124 | + |
| 125 | +Your implementation is correct when all tests pass. |
| 126 | + |
| 127 | +#### :information_source: Notes |
| 128 | +If you want to experiment with the provided application in the App.java file, you can run the following command from the terminal: |
| 129 | + |
| 130 | +```shell |
| 131 | +./mvnw -q clean compile exec:java |
| 132 | +``` |
| 133 | +``` |
| 134 | +
|
| 135 | +Or on Windows: |
| 136 | +
|
| 137 | +```bat |
| 138 | +mvnw -q clean compile exec:java |
| 139 | +``` |
| 140 | + |
| 141 | +[1]: https://docs.oracle.com/javase/21/docs/api/index.html |
| 142 | +[2]: https://junit.org/junit5/ |
| 143 | +[3]: https://maven.apache.org/ |
0 commit comments