Skip to content
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

Open
Herrekt opened this issue Apr 27, 2021 · 20 comments
Open

AY 19/20 Sem2 Qn4 : Exceptions Ordering. #389

Herrekt opened this issue Apr 27, 2021 · 20 comments
Labels
🧠 Finals Q&A Questions related to Finals

Comments

@Herrekt
Copy link

Herrekt commented Apr 27, 2021

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):

Capture
Capture1

@Herrekt Herrekt added the 🧠 Finals Q&A Questions related to Finals label Apr 27, 2021
@brendancjz
Copy link

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.

@raihanyusri
Copy link

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

@gableejh
Copy link

both works or you can use | to catch both, refer to slide 10 of lecture 6

@liongthryan
Copy link

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!

@deniselam10
Copy link

I think its fine! As long as the later exception is not more specific than the one caught previously, your code should work.

@yowyuxuan
Copy link

I did it in a similar way as well! so long it is bottom up! :)

@MatthiasTanWeiSheng
Copy link

i think both is fine. As long as you are catching the more specific exceptions first.

@yx0555
Copy link

yx0555 commented Apr 27, 2021

I did it the same way as you too, should work since the more specific exceptions are called first!

@criyu95
Copy link

criyu95 commented Apr 27, 2021

Both ways are fine, as long as the more specific exceptions are caught first

@amaslua
Copy link

amaslua commented Apr 27, 2021

It should work, i did it the same way as u! as long as more specific to less specific

@jeremycte
Copy link

Hi @Herrekt , you can view more in-depth based on my explaination here #307 but basically either way is fine. The primary goal is to catch the more specific exceptions/ child exceptions before catching the general exceptions/parent exceptions last. 😃

@linminsaw
Copy link

In general, as long as you catch the exceptions from the most specific to the most general, it will work well :)

@YuanZhengyz
Copy link

Yup as long as you don't accidentally check for a more general exception first it will be fine.

@andreathr
Copy link

I think as long as you catch the most specific exception which is the NoPermissionException its fine!

@Jjjetplane
Copy link

As long as you are catching them from the most specific to the most general it will work!

@gwajoon
Copy link

gwajoon commented Apr 27, 2021

It will work as long as you catch them in order of specificity, starting from the most specific one!

@dannytayjy
Copy link

try { 
    accessDatabase();
} catch (NoPermissionException e1) {
    System.out.print("NoPermissionException");
} catch (ElementNotFoundException e2) { 
    System.out.print("ElementNotFoundException");
} catch (WriteException e3) {
    System.out.print("WriteException");
} catch (ReadException e4) {
    System.out.print("ReadException");
} catch (TableException e5) {
    System.out.print("TableException");
} catch (DatabaseException e6) {
    System.out.print("DatabaseException");
}

@ErMingYong
Copy link

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

@W-ShiHan
Copy link

I think that both your answers work.

Personally I draw out my workings in a tree,
then you can either

  1. Check exceptions in the order of their distance away from the root (furthest away first)
  2. Start from any "leaf" of the tree and check for exceptions backwards, and repeat until all exceptions have been checked.
    (sorry for my poor handwriting)
    tree

@JWulaXia
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧠 Finals Q&A Questions related to Finals
Projects
None yet
Development

No branches or pull requests