. NET exception-zt

Source: Internet
Author: User
In. NET, if a try... catch statement is used to handle exceptions, there are a lot of code. How can I customize an error handling class? When an exception occurs, an error handling class is automatically thrown.

Btut2004 on the first floor

Yes.
If (flag = 1) throws new myexection ("hello world ");

Public myexception: exception
{
Public myexception (string msg)
{
Console. WriteLn (msg );
}
}

Zhouzh197895 on the second floor

If (flag = 1) throws new myexection ("hello world ");
Thank you! Like the preceding statements, these statements can be used before the code is run. For example, some system errors cannot be identified by data overflow, and custom error handling classes can be automatically triggered when the system is running.

Btut2004 on the third floor

Try {

All your code in hear;

}
Catch (Exception e)
{
Throw new myexception ("hello world ");
}

4 floor AhBian

The main idea is: do not directly handle exceptions for each piece of code, but use a special function class to handle these exceptions.

First of all, I disagree with this idea. Each function block and code segment have their own logic and status. Specialized function classes are not suitable and cannot handle all exceptions, in addition, if the exception is not handled in a timely manner, all subsequent Code cannot continue.

Second, there should be a special function class to handle exceptions that are not correctly handled, that is, these exceptions are unexpected exceptions in the development process.

For the second event, you can handle Application. ThreadException (note: the JIT exception debugging function must be disabled before being triggered) and AppDomain. CurrentDomain. UnhandledException.

As shown in the following code (for how to handle and implement it on your own, only one idea is provided ):
Public class ExceptionManager
{

# Region Constructors
Private ExceptionManager ()
{
}

# Endregion

Public static void Attach ()
{
Application. ThreadException + = new ThreadExceptionEventHandler (ExceptionManager. Application_ThreadException );
AppDomain. CurrentDomain. UnhandledException + = new UnhandledExceptionEventHandler (ExceptionManager. AppDomain_UnhandledException );
}

Public static void Detach ()
{
Application. ThreadException-= new ThreadExceptionEventHandler (ExceptionManager. Application_ThreadException );
AppDomain. CurrentDomain. UnhandledException-= new UnhandledExceptionEventHandler (ExceptionManager. AppDomain_UnhandledException );
}

//......

}
Call the Attach method before Application. Run, and then call the Detach method.

To disable JIT exception debugging, add the following configuration section to the configuration file:
<System. windows. forms jitDebugging = "false"/>

The preceding example is only used for WinForm applications.

Zhouzh197895 on the fifth floor

Or try... catch is used. I want to use the whole system like this, so there will be a lot of code.

Btut2004 on the 6th floor

I understand that you have to make a user-friendly interface. This feature is blocked whenever an error occurs.
Only simple and friendly error information is displayed.
I don't know how to do it. Continue to follow

Zhouzh197895 on the 7th floor

I mean the second point I mentioned by the landlord. I don't know if it is possible to use webform. I will try it.

AhBian on the eighth floor

ExceptionManager. Application_ThreadException
ExceptionManager. AppDomain_UnhandledException
The code for the two methods is not provided.

In these two methods, you can decide on how to handle unexpected exceptions. Of course, you can also display a window to tell the user that an unexpected exception has occurred. In addition, you can collect exception information (including machine information and other status information) When you click "Send Error Report", just like Microsoft's Windows Error Report) and send it to your technical support staff.

However, for AppDomain. CurrentDomain. UnhandledException events, the event parameter UnhandledExceptionEventArgs has an attribute IsTerminating. If this value is true, it indicates that a fatal Exception error has occurred. In this case, it may not be suitable for displaying an error report window. In this case, I will display a MessageBox, at the same time, the exception information is copied to the system clipboard. The responsible user can copy the exception information from the system clipboard and send it to the company's technical support staff. However, it is worth mentioning that the MessageBox I use is an enhanced message box, which has the following features: you can use the mouse to select and copy exception information (a read-only text box) in the message box. This is not the case for a common message box.

The MessageBox enhancement method comes from the following articles:
Http://msdn.microsoft.com/msdnmag/issues/02/11/CuttingEdge/

9 Floor zhouzh197895

Web. set <mermerrors mode = "RemoteOnly" defaultRedirect = "Error. aspx "/> to use Error. aspx handles all page exceptions in a unified manner, but jumps to Error. server. getLastError () does not catch any exceptions. Who knows why? How can I capture exceptions?

Baron on, Floor 0

Developing an exception handling class is good, but the problem is that it is difficult to use and use.
Exceptions include system exceptions and business exceptions. Usually we need to process the exception information, rather than simply throwing it out. However, there are so many system exceptions that it is too troublesome to perform precise processing.
Generally, most exceptions are caused by the program being not strictly tested. It is necessary to add the entered data to the program. Therefore, we usually only need to handle business exceptions and then some common system exceptions.

Hning123, floor 1

1. Set <customErrors mode = "On" defaultRedirect = "Error. aspx"/> in web. config.
2. Add the Application_Error event in Global. asax. vb
Sub Application_Error (ByVal sender As Object, ByVal e As EventArgs)
If Not Context Is Nothing And Context. IsCustomErrorEnabled Then
Server. Transfer ("cerror. aspx", False)
End If
End Sub
3. add error. aspx. vb
Private Sub Page_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) handles MyBase. Load
Dim ex As Exception = Server. GetLastError. GetBaseException
Label1.Text = ex. Message
End Sub
Try it !!!!!! Don't forget to throw your exception ....

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.