Throw vs. MsgBox?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Throw vs. MsgBox??

If we can MANUALLY "throw" exceptions by testing for specific conditions & then adding code to "throw" them, why not just test for the condition, then execute code to handle it (e.g. MsgBox with error msg)? Why even create a list of "throws"? Why not just test for the conditions, call handlers if they occur, otherwise execute the code block?

8th Sep 2016, 7:30 PM
Raymond Enix
Raymond Enix - avatar
2 Answers
+ 1
You may have several lines of code that can throw the same exception (or have a unique catch for several exceptions, or even all, in a block that can throw exceptions at different points), doing it your way would be code repetition in that case. Also, after catching an exception, the execution will continue directly after the try/catch block (or at the finally block if there is one).
8th Sep 2016, 8:08 PM
Zen
Zen - avatar
+ 1
The code may be a library that had no knowledge of how the error should be handled. But, the library code can throw an exception, so that the calling code (in the main program) can catch it and handle the situation by logging the error or displaying the error message.
7th Jan 2017, 7:25 PM
ifl
ifl - avatar