Handling these Exceptions
Handling these exceptions doesn't take much, all it needs is a try....catch...finally code block. See here as I overcome this error in the same example, try using realy large numbers and it comes up with -
An error has occurred, your numbers may be too large
Now, that error is a bit more useful to the user. Here is its code for it.
<script language="vb" runat=server>
Sub multiply(sender as object, e as system.eventargs)
Try 'Start try block
lbl.Text = CInt(num1.Text) * CInt(num2.Text) 'Code to try
Catch excp as system.exception 'Catch the exception if the try doesn't work
lbl.Text = "An error has occured, your numbers may be too large" 'Handle the exception
End Try 'End try block
End Sub
</script> |
Now you should be able to see the try block at work. Here is the syntax for it -
Try
'Code to Try. If try fails you can catch the failure
Catch [var] as [thing to catch]
'Handle the problem here
Finally
'Close off any loose ends
End Try |
The most common thing for the Catch statement (and the only one we'll be using for this article is Catch excep as System.Exception. Now, lets take a closer look at the System.Exception.