-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AY 19/20 Sem2 Qn4 : Exceptions Ordering. #389
Comments
Hi, I believe either way is fine. The more common approach will be to the finish catching the child exception of a branch. As long as you are always catching the next most specific exception, you're good. |
Either way is fine. If the exceptions have a parent-child relationship, then the catch blocks must be sorted by the most specific exceptions first, followed by the most general ones within the group itself |
both works or you can use |
I think your method works fine! If you're writing the code in an IDE, most IDEs flag unreachable code blocks (ie. you tried to catch a more general exception before catching the specific exception). Since prof said we can write our answers for finals using any IDEs (iirc), it might not be a bad strategy to just rely on those! |
I think its fine! As long as the later exception is not more specific than the one caught previously, your code should work. |
I did it in a similar way as well! so long it is bottom up! :) |
i think both is fine. As long as you are catching the more specific exceptions first. |
I did it the same way as you too, should work since the more specific exceptions are called first! |
Both ways are fine, as long as the more specific exceptions are caught first |
It should work, i did it the same way as u! as long as more specific to less specific |
In general, as long as you catch the exceptions from the most specific to the most general, it will work well :) |
Yup as long as you don't accidentally check for a more general exception first it will be fine. |
I think as long as you catch the most specific exception which is the NoPermissionException its fine! |
As long as you are catching them from the most specific to the most general it will work! |
It will work as long as you catch them in order of specificity, starting from the most specific one! |
|
rule of thumb is to just catch the more specific errors than the more general errors. Arrangement not be a problem unless you catch a more general error before a more specific error instead |
I think that both your answers work. Personally I draw out my workings in a tree, |
idea is go from the most specific exception, if two exception are at the same level, the the order does not matter. this can ensure all exceptions are caught and are caught in the most specific one |
Summary
I know that we should focus on the more specific exceptions to the least specific ones, but if they branch off from the same parent exception, can we clear one whole child exception first before starting on the next?
Description
For example, if we were to catch NoPermissionException, then catch WriteException, before catching ElementNotFoundException and Read Exception, will it be problematic/wrong in this case?
Screenshots (if any):
The text was updated successfully, but these errors were encountered: