Repository navigation
Add new exercise Error Handling - #1338
Conversation
sjwarner-bp
left a comment
There was a problem hiding this comment.
Wow, another exercise!
I've left a few comments from a quick glance - I'll have a better chance to look at this again tomorrow (if I'm not beaten to it!) 🙂
| "difficulty": 4, | ||
| "slug": "error-handling", | ||
| "topics": [ | ||
| "exception_handling" |
There was a problem hiding this comment.
Any other suggestions for possible topics? The list of topics can be found here
There was a problem hiding this comment.
Yes, optional_values
| throw new Exception(); | ||
| } | ||
|
|
||
| public void handleErrorByThrowingExceptionWithDetailMessage(String message) throws Exception { |
There was a problem hiding this comment.
public methods could be changed to package-private 🙂
| @@ -0,0 +1,24 @@ | |||
| # Error Handling | |||
There was a problem hiding this comment.
Has this been generated by configlet? I'd expect the metadata to show up
There was a problem hiding this comment.
Yes, I generated it with configlet.
There was a problem hiding this comment.
Perfect! On a closer look, the normal metadata I would expect is not actually in the problem-specifications yet!
|
|
||
| class ErrorHandling { | ||
|
|
||
| public void handleErrorByThrowingException() throws Exception { |
There was a problem hiding this comment.
Use of public in this file can also be changed to package private!
Also, I think we can remove throws Exception from these - it might give a little bit too much away to the users 😉 You should still be able to correctly throw UnsupportedOperationException even without this 🙂
|
Another thing to consider is that in Java we can make brand new exception classes - if it isn't too excessive (feel free to tell me it is overly-complex or out of specification), we could even ask a user to make their own custom exception! @exercism/java what do you think of this suggestion? |
|
As you say maybe is a bit excessive. Python and C# tracks don't ask the user to implement a new exception class. |
FridaTveit
left a comment
There was a problem hiding this comment.
Thanks for adding this exercise @crivasgomez! 🙂
I've added a few comments, feel free to ask questions or disagree with me 🙂
|
|
||
| class ErrorHandling { | ||
|
|
||
| void handleErrorByThrowingException() throws Exception { |
There was a problem hiding this comment.
In general it's considered bad practice to throw or catch just Exception. You should either use one of the built in unchecked exceptions (e.g. IllegalArgumentException) or you should construct your own checked exception which extends Exception. This has been done for other exercises which don't just focus on error handling so I definitely don't think it's too much to ask in this exercise. On the contrary, I think it's an important thing to learn about error handling in Java 🙂 Since you've estimated this exercise as difficulty 4, you would need to provide starter implementation for the custom exception, so I don't think it should be that hard for the user to implement 🙂 What do you think? 🙂
There was a problem hiding this comment.
Good point 🙂I will add a test case for throwing a custom exception and provide starter implementation 🙂
There was a problem hiding this comment.
Great, thank you! Those new methods look great! 🙂
However, I would still disagree with keeping these methods which throw Exception. I realise I didn't explain why it's bad practice to throw or catch Exception, sorry about that! The reason is that Exception is the superclass of all exceptions. So if you say your method throws Exception then you don't give any details about the exception your method might throw, it could throw any exception at all. Even worse, when you catch Exception you catch all exceptions including runtime exceptions you might not want to handle. Therefore in Java you should always throw and catch specific exception classes that accurately describe the error that has occurred or the error that you're trying to handle. This stackoverflow question has some more information. Does that make sense? 🙂
It might actually be a good idea to add some hints to this exercise (in a hints.md file in the .meta directory, see flatten-array for an example) explaining the difference between checked and unchecked exceptions (possibly linking to https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/), when you should use unchecked exceptions (possibly linking to https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html) and why you shouldn't throw or catch Exception (possibly linking to the stackoverflow question). Do you want to do that @crivasgomez? That's absolutely not a requirement to get this exercise merged, if you think you have enough work with the rest of the exercise then I can just open a separate issue for adding a hints file 🙂
| errorHandling = new ErrorHandling(); | ||
| } | ||
|
|
||
| @Test(expected = Exception.class) |
There was a problem hiding this comment.
We've decided to use ExpectedException on this track instead of @Test(expected = ...). This is because you can do more with ExpectedException such as assert what the error message is (which would be useful in your next test) 🙂 See the phone number tests for an example of how it's used 🙂
There was a problem hiding this comment.
OK. I didn't notice that guideline 🙂
| "exception_handling", | ||
| "optional_values" | ||
| ], | ||
| "unlocked_by": "saddle-points", |
There was a problem hiding this comment.
Why do you think this should be unlocked by saddle-points? I would expect it to be unlocked by an exercise that also deals with exceptions, for example perfect-numbers? 🙂
There was a problem hiding this comment.
I think triangle would suit better (it also deals with exceptions) 🙂
FridaTveit
left a comment
There was a problem hiding this comment.
Thank you for making those changes @crivasgomez! This exercise is looking really good! 🙂
|
|
||
| class ErrorHandling { | ||
|
|
||
| void handleErrorByThrowingException() throws Exception { |
There was a problem hiding this comment.
Great, thank you! Those new methods look great! 🙂
However, I would still disagree with keeping these methods which throw Exception. I realise I didn't explain why it's bad practice to throw or catch Exception, sorry about that! The reason is that Exception is the superclass of all exceptions. So if you say your method throws Exception then you don't give any details about the exception your method might throw, it could throw any exception at all. Even worse, when you catch Exception you catch all exceptions including runtime exceptions you might not want to handle. Therefore in Java you should always throw and catch specific exception classes that accurately describe the error that has occurred or the error that you're trying to handle. This stackoverflow question has some more information. Does that make sense? 🙂
It might actually be a good idea to add some hints to this exercise (in a hints.md file in the .meta directory, see flatten-array for an example) explaining the difference between checked and unchecked exceptions (possibly linking to https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/), when you should use unchecked exceptions (possibly linking to https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html) and why you shouldn't throw or catch Exception (possibly linking to the stackoverflow question). Do you want to do that @crivasgomez? That's absolutely not a requirement to get this exercise merged, if you think you have enough work with the rest of the exercise then I can just open a separate issue for adding a hints file 🙂
| @Test | ||
| public void testThrowCustomCheckedException() { | ||
| try { | ||
| errorHandling.handleErrorByThrowingCustomCheckedException(); |
There was a problem hiding this comment.
You should be able to deal with this in the same way as the other tests, i.e.
thrown.expect(CustomCheckedException.class);
The same goes for the next three tests🙂
There was a problem hiding this comment.
The purpose of the first test is to test the users capacity for throwing an Exception. The user will have to become aware about the need of adding throws Exception to the method signature. There is no way to bypass the declaration of throws Exception for the user as long as the test is expecting an instance of Exception.class. I could remove this test and replace it with other two testThrowCheckedException and testThrowUncheckedException, what do you think? 🙂
I think that would be a good idea to add these hints 🙂
There was a problem hiding this comment.
Yes, my problem is with having the test expect the user to throw just Exception and not a more specific class. Replacing this test with the other two sounds like a great idea 🙂 Though maybe the IllegalArgumentException tests should be before the custom exception tests? That way the user can first use an exception class which already exists before having to worry about custom exceptions. I don't think it matters too much though, either is fine 🙂
| public class ErrorHandlingTest { | ||
|
|
||
| private ErrorHandling errorHandling; | ||
| private Set<Class> checkedExceptions; |
There was a problem hiding this comment.
Why do you need these two sets? You've already provided the starter implementation for the exception you want them to throw (CustomCheckedException and CustomUncheckedException), so you know which class it should be 🙂
There was a problem hiding this comment.
I added them before providing the implementation and I forgot them 😅 Sets removed 🙂
|
👍 to adding some hints - I think that could be super useful |
There was a problem hiding this comment.
This looks great @crivasgomez! Thank you for making all these changes so thoroughly and quickly! 🙂
I've added some comments to the hints. However, everything is great as it is so if you feel you've done enough on this exercise then I'm happy to merge it and open a seperate issue for adding some more details to the hints 🙂
|
|
||
| In Java, there are two types of exceptions: checked and unchecked exceptions. | ||
|
|
||
| Checked exceptions are the exceptions that are checked at compile time. |
There was a problem hiding this comment.
Maybe you could expand a bit on this since it might not be obvious to a beginner what "checked at compile time" means? 🙂 I think it's important to make clear:
- The practical implications of a checked exception, i.e. that you have to declare it in the method signature of any method that can throw a checked exception, and handle or rethrow it when calling any method that can call a checked exception.
- That this is because checked exceptions are meant to be handled at runtime, i.e. they are errors you can recover from.
- Examples of where they are used. 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. It's an alternative to returning
nullor an error code. A checked exception is better than those alternatives because it forces the user of the method to consider the error case.
Does that make sense? 🙂
|
|
||
| Checked exceptions are the exceptions that are checked at compile time. | ||
|
|
||
| Unchecked exceptions are the exceptions that are not checked at compiled time. |
There was a problem hiding this comment.
Again it might be worth expanding on this a bit to explain what this means (that you don't have to add the exception to method signatures and you don't have to handle it) and explain what kind of errors it's meant to be used for (any that can't be handled at runtime, e.g. running out of memory) 🙂
FridaTveit
left a comment
There was a problem hiding this comment.
These hints are looking great, thanks @crivasgomez! I've added a few grammatical suggestions 🙂
|
|
||
| ### Practical implications | ||
|
|
||
| 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 call a checked exception. |
There was a problem hiding this comment.
I would probably say "that can throw a checked exception" rather than "call" 🙂
|
|
||
| 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 call a checked exception. | ||
|
|
||
| This is because checked exceptions are meant to be handled at runtime, ie., they are errors you can recover from. |
There was a problem hiding this comment.
I think it should be "i.e." with a full stop between i and e, and without a comma after it 🙂
|
|
||
| ### Practical implications | ||
|
|
||
| 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 call an unchecked exception. |
There was a problem hiding this comment.
Again, I would say "throw" rather than "call" an exception 🙂
|
|
||
| ### Examples of where they are used | ||
|
|
||
| Unchecked exceptions are mean to be used for any error than can't be handled at runtime, e.g., running out of memory. No newline at end of file |
There was a problem hiding this comment.
I don't think there should be a comma after "e.g." 🙂
FridaTveit
left a comment
There was a problem hiding this comment.
Looks great! Thank you for doing all this @crivasgomez!
🙂 This will be a great new exercise to add to the track! Error handling is so important and it's one of my favourite parts of Java compared to other languages, so it's great to have an exercise dedicated to it🙂
* 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
Hello!
I added Error Handling exercise 🙂
As its problem specification says I have to refer to the tests for Java track to see what's exactly required, but Java tests can't be generated, so i readed the tests for C# and Python and I come up with these.
Reviewer Resources:
Track Policies