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?
2 odpowiedzi
+ 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).
+ 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.