What is the Usage of Throw in a Catch File????
Throw
Collapse
X
-
Do you mean code like the following?
It's called rethrowing the exception and you might do that if you decide that the currenty try - catch block doesn't know how to deal with that particular exceptionCode:Try ' Do stuff here Catch DivByZero As System.DivideByZeroException ' Catch divide by zero exceptions Catch OtherException As Exception Throw OtherException End Try
In this case rethrowing the exception causes it to be passed up to the next try - catch block in the chain. So, say the above code was a function you had written called DoStuff() and you've wrapped the call to DoStuff() in another try - catch block, the rethrown exception would get passed back to that block. -
Re-throwing an exception isn't the only use for a Throw.
If you are developing a control and you need to inform the calling code that something has gone wrong you could throw an exception.
For instance, if your Person object requires a name before doing something with it, you could throw an exception if the calling code attempts to use the Person object before setting a name. In this case, designing a custom exception is a good idea to indicate that it is a problem originating from the Person object.
Check out this article for more information on designing a custom exception.Comment
Comment