Skip to content

Add new exercise Error Handling - #1338

Merged
FridaTveit merged 11 commits into
exercism:masterfrom
crivasgomez:new-exercise-error-handling
Feb 28, 2018
Merged

FridaTveit merged 11 commits into
exercism:masterfrom
crivasgomez:new-exercise-error-handling

Conversation

@crivasgomez

Copy link
Copy Markdown
Contributor

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

@sjwarner-bp sjwarner-bp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!) 🙂

Comment thread config.json Outdated
"difficulty": 4,
"slug": "error-handling",
"topics": [
"exception_handling"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any other suggestions for possible topics? The list of topics can be found here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, optional_values

throw new Exception();
}

public void handleErrorByThrowingExceptionWithDetailMessage(String message) throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public methods could be changed to package-private 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True 🙂

@@ -0,0 +1,24 @@
# Error Handling

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been generated by configlet? I'd expect the metadata to show up

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I generated it with configlet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 🙂

@sjwarner-bp

Copy link
Copy Markdown
Contributor

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?

@crivasgomez

Copy link
Copy Markdown
Contributor Author

As you say maybe is a bit excessive. Python and C# tracks don't ask the user to implement a new exception class.

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 🙂I will add a test case for throwing a custom exception and provide starter implementation 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

@crivasgomez crivasgomez Feb 27, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I didn't notice that guideline 🙂

Comment thread config.json Outdated
"exception_handling",
"optional_values"
],
"unlocked_by": "saddle-points",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think triangle would suit better (it also deals with exceptions) 🙂

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making those changes @crivasgomez! This exercise is looking really good! 🙂


class ErrorHandling {

void handleErrorByThrowingException() throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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🙂

@crivasgomez crivasgomez Feb 28, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added them before providing the implementation and I forgot them 😅 Sets removed 🙂

@sjwarner-bp

Copy link
Copy Markdown
Contributor

👍 to adding some hints - I think that could be super useful

@FridaTveit FridaTveit left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

Comment thread exercises/error-handling/.meta/hints.md Outdated

In Java, there are two types of exceptions: checked and unchecked exceptions.

Checked exceptions are the exceptions that are checked at compile time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. That this is because checked exceptions are meant to be handled at runtime, i.e. they are errors you can recover from.
  3. 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 null or 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? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK 🙂

Comment thread exercises/error-handling/.meta/hints.md Outdated

Checked exceptions are the exceptions that are checked at compile time.

Unchecked exceptions are the exceptions that are not checked at compiled time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These hints are looking great, thanks @crivasgomez! I've added a few grammatical suggestions 🙂

Comment thread exercises/error-handling/.meta/hints.md Outdated

### 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably say "that can throw a checked exception" rather than "call" 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups 🙂

Comment thread exercises/error-handling/.meta/hints.md Outdated

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be "i.e." with a full stop between i and e, and without a comma after it 🙂

Comment thread exercises/error-handling/.meta/hints.md Outdated

### 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I would say "throw" rather than "call" an exception 🙂

Comment thread exercises/error-handling/.meta/hints.md Outdated

### 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there should be a comma after "e.g." 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 🙂

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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🙂

@FridaTveit
FridaTveit merged commit be50978 into exercism:master Feb 28, 2018
@crivasgomez
crivasgomez deleted the new-exercise-error-handling branch February 28, 2018 18:01
hgvanpariya pushed a commit to hgvanpariya/java that referenced this pull request May 13, 2018
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants