Exceptions in ASP.NET
 
Published: 08 Sep 2001
Unedited - Community Contributed
Abstract
ASP.NET uses VB.NET as language of choice and so it inherits all of VB's new features. One of these is exception handling with the System.Exception class. This new feature allows ASP developers to debug at runtime rather than have to build tricky coding to try and catch the different types of errors that they think will happen at runtime. Because ASP.NET uses VB.NET all of these samples can be moved across to a VB.NET application with ease.
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21109/ 28

Introduction

Exceptions in ASP.NET

Published 09/08/01

Introduction

ASP.NET uses VB.NET as language of choice and so it inherits all of VB's new features. One of these is exception handling with the System.Exception class. This new feature allows ASP developers to debug at runtime rather than have to build tricky coding to try and catch the different types of errors that they think will happen at runtime. Because ASP.NET uses VB.NET all of these samples can be moved across to a VB.NET application with ease.

Exceptions?

Exceptions?

An exception is an error. Its not a compile time error though, but a runtime error. Runtime errors are ones that don't occur at compile-time because they aren't anticipated to happen. The best way to show you this is through an example. Here, is a simple page that does multiplication. Try it out. You'll see that it can multiply to some degree of accuracy. Now enter in two numbers larger than 2147483647 and -2147483647. You get an exception like the one below.

Exception of type System.OverflowException was thrown.

This happens because integers only can store numbers below 2147483647 and above -2147483647. I converted the numbers to Integers beforehand if you ask why I didn't use a larger storage medium. The same error would have occurred with a Double or Decimal. When the program came across these large numbers it gave up and threw an exception. Here is the code for the page -

<html>
<body>
<script language="vb" runat=server>

Sub multiply(sender as object, e as system.eventargs)

lbl.Text = CInt(num1.Text) * CInt(num2.Text)

End Sub

</script>
<form runat=server>
<asp:textbox id="num1" runat=server ontextchanged="multiply"/> x <asp:textbox id="num2" runat=server ontextchanged="multiply"/> = <asp:button runat=server/><p><asp:label id="lbl" runat=server/>
</form>
</body>
</html>

Handling Exceptions

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.

System.Exception

System.Exception

The System.Exception class is the generic class where all exceptions go. There are sub-classes for more specific errors like ArgumentException and NotSupportedException. We won't go into all of these in detail, just to let you know that system.exception isn't the only one. We have seen the OverflowException happen and we handled a generic exception. Better code practise dictates that we should handle a more specific error. Here, is where we handle the overflow and the generic with two Catch blocks.

<script language="vb" runat=server>
Sub multiply(sender as object, e as system.eventargs)
Try
lbl.Text = CInt(num1.Text) * CInt(num2.Text)
Catch excp as system.overflowexception       'Catch overflow errors
lbl.Text = "Those numbers are too large"
Catch excp as System.Exception               'Catch any other error
lbl.Text = "An error has occurred"
End Try
End Sub
</script>

Now, you can get the overflow error and if you want to get the other one, try entering in some letters into one of the text boxes.



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 2:54:19 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search