To add an assertion to a class

Source: Internet
Author: User
Tags httpcontext stack trace tostring
Briefly:
Automatically throws an exception after an exception is caught with a try, and clears the exception with Server.ClearError () in the Global.asax.vb Application_Error. Use System.Web.HttpContext.Current.Response to output JavaScript code to implement informational reminders and return to the previous page.


Detailed Description:
For example, the method of connecting to a database is made into a class Cdbconn.vb. The problem is that when the database connection fails, the system automatically throws the exception that failed the connection.
Since the action of a program calling the object of the class is unpredictable, it is best to finish the program after the exception has been processed and return to the previous page.
Catching exceptions in a class can also be a try, but after processing it is a bit cumbersome to end the daemon, searching the forum for a connection that Shi greatly:

How To:create Custom Error Reporting the Pages in asp.net by Using Visual C #. NET
http://support.microsoft.com/default.aspx?scid=kb; en-us;306355
The vb.net join IS
http://support.microsoft.com/default.aspx?scid=kb; en-us;308132

The practice has come to the following scenario:
In the class file Cdbconn.vb, there are
Try
Dbconn. Open ()
Catch ex as Exception
' System.Web.HttpContext.Current.Response.Write (Win.msgbox (strmsg_nonedatabase))
Throw New System.Exception (strmsg_nonedatabase)
End Try
After the exception is intercepted, an exception is thrown. This sounds a bit of a problem, the main purpose is that may need to deal with the trouble caused by the exception, there is nothing to deal with, so just customize an exception information to the system users.

Add related actions in Application_Error in Global.asax.vb
Sub Application_Error (ByVal sender as Object, ByVal e as EventArgs)
' Fires when an error occurs
Dim objerr as Exception = Server.GetLastError (). GetBaseException ()
Dim err as String = "Error Caught in Application_Error event" & _
System.Environment.NewLine & _
"Error in:" & Request.Url.ToString () & _
System.Environment.NewLine & _
"Error message:" & ObjErr.Message.ToString () & _
System.Environment.NewLine & _
"Stack Trace:" & ObjErr.StackTrace.ToString ()

' EventLog.WriteEntry ("Sample_WebApp", err, EventLogEntryType.Error)
Server.ClearError ()
System.Web.HttpContext.Current.Response.Write (' <script language= ' JavaScript ' >alert (' "& ObjErr.Message.ToString & "');</script>")
System.Web.HttpContext.Current.Response.Write ("<script language= ' JavaScript ' >history.go ( -1); </script > ")
End Sub

Here ' EventLog. WriteEntry ("Sample_WebApp", err, EventLogEntryType.Error) will throw the exception normal, and will not continue to execute downward, so the comment dropped, MSDN on the cuff, is the version of the problem?

The most important thing is server.clearerror (), without which the exception will be thrown normally. As of this system will return a piece of white paper, nothing of course is not, so the following with response output prompts, and return to the previous page. Solve this problem.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.