+ 2

Can we use throw outside try or catch block ?

Usually we use throw when we want to raise our own exception in program and handle it, but what happens when we use throw wherever required without try or catch blocks just like if-else?

24th Apr 2020, 1:59 PM
Nikhil Maroju
Nikhil Maroju - avatar
1 Answer
+ 1
Probably your program will just run unnecessarily slow. Usually with if and else, switch or ternary operator, you compare conditions or even just values. Doesn't get any easier than that. So you should use exception handling for cases where that's not good enough, where you can't predetermine conclusively what's going to happen.
24th Apr 2020, 2:49 PM
HonFu
HonFu - avatar