Error Trapping in ASP.NET

A new feature of ASP.Net is the Try/Catch block. If nothing else, it makes trapping errors much more elegant, and cleaner looking. Whenever your app encounters an error, it pretty much crashes, with a pretty ugly output for the end user. Sometimes, the user can understand the resulting page – - sometimes, not. Here, we will attempt to give you an Overview of the Try/Catch block, in order to guard against possible code errors.

Naturally, this isn’t a way for you to blow off your total development, but it is a way for you to catch unexpected errors without crashing the actual execution of the page. You’ll still want to check for logical, error-producing situations, and code around them, depending on your particular application’s circumstances, as well as using ASP.Net Validation Controls.

The Try block, of course, starts with ‘TRY’. Then, it ends with ‘END TRY’. Inside this block, you will ‘try’ or attempt to execute your code. Inside this block, you’ll want to add a ‘CATCH’ section. Here, this basically tells your page what to do when it encounters an error. An example of this would be:

Try ' do your code execution here Catch ex as Exception lblError.Text = "An error has occured/" lblError.Text+= ex.Message End Try

Continues…