For Microsoft Visual Basic .NET versions of this article, see
315965.
This task content
Summary
Require structured abnormal processing capture exception to capture multiple exceptions, exception completion code list
Capture abnormal capture multiple exceptions
SUMMARY This article describes how to use structural exceptions in Microsoft Visual C # .NET.
Back to top
The following table summarizes the recommended hardware, software, network architecture, and service pack required:
Visual C # .net
This article assumes that you are familiar with the following topics:
Visual C # .NET Microsoft Visual Studio .NET
Back to top
Structured exception handling Visual C # .NET provides structural exception processing, which provides a powerful and more readable method of handling errors. Structured Abnormal Processing allows you to nest the error handler in other error handles in the same process. Structured abnormal processing uses a similar to
IF ... Else ... END IF statement Module syntax. This makes the Visual C # .NET code more readable and easier to maintain. You can use the combination of exception handling statements to handle exceptions in Visual C # .NET:
The basic syntax of the TRY Catch Finally Throw structure is as follows:
Try
{
// code That is expected to raise an exception.
}
Catch (Exception E)
{
// Code That Can Handle An Error.
}
Finally
{
// Code to do any final cleanup.
}
You can
Any valid Visual C # code is included in the TRY module, or another
TRY module or
TRY module hierarchy. When anything is abnormal, the public language runtime will find the most recent layer of this code.
The TRY module does not have to run any additional code rows. This control is then passed to match
Catch module (if any) and pass to associated
Finally module. You can also specify multiple
Catch statement so that each
Catch modules handle a specific error.
Back to top
Capture exception
Click Start, point to Programs, point to "Microsoft Visual Studio .Net", and then click Microsoft Visual Studio .NET. On the File menu, point to "New" and click Project. In the New Project dialog, follow these steps:
Below "Project Types", click Visual C # items. Under Templates, click Console Applications. Type MyconsoleApp in the Name box. In the Location box, type C: /, and then click OK. Add the following code to the main () function: int a = 0;
INT b = 10;
INT C = 0;
Try
{
A = B / C;
}
Catch (Exception E)
{
Console.writeline ("a run-time error occurred.");
}
Finally
{
Console.readline ();
} To run the application, click Start on the Debug menu. This code will try to make a number of 0. This operation is invalid and will result in errors that are divided by zero. However, the catch module can capture this error, and the "Console" window will display the following error message:
A Run-time error Occurre
Close the Console window.
Back to top
Capturing multiple exceptions This section describes how to use multiple Catch statements to handle different errors.
Open your "Console Application" project created in this article "Capture Exception" section. Replace the existing code in the main () function to the following code: int A = 2147483647;
INT b = 0;
INT C = 0;
Try
{
a = checked (A 1);
}
Catch (DivideByzeroExcection E)
{
Console.writeline ("Error: Divide By Zero", E.MESSAGE);
}
Catch (OverflowException E)
{
Console.writeline ("Error: Overflow", E.MESSAGE);
}
Finally
{
Console.readline ();
} This code includes two CATCH modules:
A CatCH module capturess the previous use of zero to divide an error. A Catch module captures a new overflow error. To run this application, click Start on the Debug menu. The Console window displays the following error message:
Error: Overflow
Close the Console window. Because you don't always expect everything that will appear, you can add a Catch module for all exceptions that cannot be expected. For example, add the following code to capture any unpredictable errors before the Finally statement: Catch (Exception E)
{
Console.Writeline ("Error:", E.MESSAGE);
} On the File menu, click Close Solution.
Back to top
Exit anomalous structured exception handler
Catch statement to capture exceptions. Use structured abnormal processing, it may also trigger an exception. For example, you may find
A exception is triggered during the Property set process, because you may need to trigger an error message when you violate a business rule.
Click Start, point to Programs, point to "Microsoft Visual Studio .Net", and then click Microsoft Visual Studio .NET. On the File menu, point to "New" and click Project. In the New Project dialog, follow these steps:
Below "Project Types", click Visual C # items. Under Templates, click Console Applications. Type MyNewConsoleApp in the Name box. In the Location box, type C: /, and then click OK. On the Projects menu, click Add Category. In the Name box of the Add New Item dialog, type clsperson.cs, and then click Open. Add the following code to the Clsperson class: public clsperson ()
{
}
Private int mintage;
Public int value;
Public Int Age
{
get
{
Age = mintage;
Return Age;
}
set
{
IF (Value> 0)
Mintage = value;
Else
Throw New ArgumentException ("Age Cannot Be NEGATIVE.");
}
} This code creates an AGE property. Since a person's age cannot be negative, if the user attempts to set the AGE property to a number of less than 0, there will be an error. In the main () function of class1.cs, add the following code: CLSPERSON P = New Clsperson (); TRY
{
P.AGE = -1;
}
Catch (Exception E)
{
Console.writeLine (E.MESSAGE);
}
Finally
{
Console.readline ();
} To run this application, click Start "on the Debug menu. The Console window displays the following error message:
Age Cannot Be Negative
Close the Console window.
Back to top
Complete code list
Capture exception
Using system;
Namespace myconsoleapp
{
Class class1
{
[Stathread]
Static void main (string [] args)
{
INT A = 0;
INT b = 0;
INT C = 0;
Try
{
A = B / C;
}
Catch (Exception E)
{
Console.writeline ("a run-time error occurred.");
}
Finally
{
Console.readline ();
}
}
}
}
Capture multiple exceptions
Using system;
Namespace myconsoleapp
{
Class class1
{
[Stathread]
Static void main (string [] args)
{
INT A = 2147483647;
Try
{
a = checked (A 1);
}
Catch (DivideByzeroExcection E)
{
Console.writeline ("Error: Divide By Zero", E.MESSAGE);
}
Catch (OverflowException E)
{
Console.writeline ("Error: Overflow", E.MESSAGE);
}
Catch (Exception E)
{
Console.Writeline ("Error:", E.MESSAGE);
}
Finally
{
Console.readline ();
}
}
}
}
Abnormal
Using system;
Namespace mynewconsoleapp
{
Class class1
{
[Stathread]
Static void main (string [] args)
{
CLSPERSON P = New Clsperson ();
Try
{
P.AGE = -1;
}
Catch (Exception E)
{
Console.writeLine (E.MESSAGE);
}
Finally
{
Console.readline ();
}
}
}
Public Class Clsperson
{
Public clsperson ()
{
}
Private int mintage;
Public int value;
Public Int Age
{
get
{
Age = mintage;
Return Age;
}
set
{
IF (Value> 0)
Mintage = value;
Else
Throw new ArgumentException ("Age Cannot Be Negative.");
}
}
}
Back to top
Refer to more information, please visit the Microsoft Web site below:
Exception management application block for .nethttp: //msdn.microsoft.com/library/default.asp? URL = / library / en-us / dnbda / html / emab-rm.asp
How do i catch an exception? Http://samples.gotdotnet.com/quickstart/howto/doc/catch.aspx
How do I throw an exception? Http://samples.gotdotnet.com/quickstart/howto/doc/throw.aspx
Back to top
The information in this article applies to:
Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002)
Recent Updated: 2004-1-12 (1.0) Keywords: kbprogramming kbexcepthandling KbhowTomaster KB816157 KBAUDDEVELOPER