+ 6

How can i order the catch blocks?

Can anyone help me with explaining this.. "All catch blocks should be ordered from most specific to most general"

19th Jan 2017, 5:58 AM
Am²ar Yas²er
Am²ar Yas²er - avatar
4 Réponses
+ 3
depends on what would be your try code about.
19th Jan 2017, 6:37 AM
Nahuel
Nahuel - avatar
+ 5
There is no specific criteria to order them? Order is by logic and depends on the developer?
19th Jan 2017, 6:07 AM
Am²ar Yas²er
Am²ar Yas²er - avatar
+ 3
indeed, you can catch first (Exception e), and then a "indexoutorange". You must order the most likely to happend, and a general exception wich will catch any exception that would not be catched by the specific ones.
19th Jan 2017, 6:02 AM
Nahuel
Nahuel - avatar
0
You always order the catch blocks in order of childhood. The exceptions higher up are of a more specific class Exception then further down. If you don't order them correctly most likely you will get a compile error because if a really general exception is caught the compiler treats any other catch blocks as incorrect code and flags it for error.
4th Nov 2017, 7:47 PM
Saud Al-Awadhi
Saud Al-Awadhi - avatar