+ 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"
4 odpowiedzi
+ 3
depends on what would be your try code about.
+ 5
There is no specific criteria to order them?
Order is by logic and depends on the developer?
+ 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.
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.