C # Debugging and error handling

Source: Internet
Author: User
Tags error handling exception handling

The best programmers will make mistakes in programming, not to mention those of us beginners; good programmers will find bugs and modify them. Sometimes there are some subtle bugs in the program that we have to track code to debug when we can't find it.

I. Debugging in Visual Studio

When you run a program in debug mode, you run the code that is written. The information about the debugger is recorded in the process of executing each line of code vs. pdb format files that are stored in the debug directory.

Writing text to the Output window during run time is very simple. You can use two commands at this point:

Debug.WriteLine ()

Trace.WriteLine ()

The two command functions are used in much the same way, except that the first command runs in debug mode and the second is also available for the Publisher. Reference the two functions and add in the namespace: using System.Diagnostics;

static int Maxnum (int[] a)//

{

Debug.WriteLine ("Sort array begin");

int max = a[0];

int index;

for (int i = 1; i < a.length; i++)

{

if (Max < a[i])

{

max = A[i]; Break Point Design

index = i;

}

}

Trace.Write ("The index of the bigest number is");

return Max;

In fact, the problem of debugging can be solved by themselves, only those who specialize in the implementation of the program to run the process of people will pay attention to the process of debug, in general, we found that the program can run without errors are not concerned about the specific implementation process.

Two. Exception handling.

There are exceptions when the program is running, such as when you open the file to determine if the file exists to open, and if it does not exist, an exception can cause the program to abort. If an exception is possible, use the method to process it. To ensure that the program works correctly.

Try

{

< execution process that may contain exceptions >

}

catch (Exception catch exception)

{

< exception handling >

}

What will finally{< eventually do >

}

Type of exception: 1 Basic Exception.2:application Exception class

3 exception classes related to parameters: ArgumentException Class Format Exception class:

4: Exception Memberaccessexception class related to member access, which includes three seed classes

Fileaccessexception class: Methodaccessexception class:
MissingMemberException class:

The examples that come down are very common in the process of data processing.

int x1 = 40;

int x2 = 0;

int x3;

Label re = new label ();

Re. Text=convert.tostring (X1/X2)

Execution results are

The Try---catch---Finally the exception handling pattern is executed in this way.

Try is what the program intends to do, catch is an exception that may occur during a try, and if there are exceptions and the type of exception and catch, Catch handling Exceptions. Finally, the code executes regardless of whether there are exceptions. There are, of course, multiple catch to handle multiple exceptions that may occur during the execution of the capture program.

There's not much to say here. The following is a list of the different exception types that exist during C # processing.
Memberaccessexception Access Error: Type member cannot be accessed
ArgumentException parameter error: Invalid parameter for method
ArgumentNullException parameter is null: pass an unacceptable null parameter to the method
ArithmeticException calculation error: Due to mathematical operations caused by the exception, wide coverage.
ArrayTypeMismatchException array types do not match

DivideByZeroException by 0 apart
FormatException parameter is not in the correct format
IndexOutOfRangeException Index is out of range, less than 0 or larger than the index of the last element
InvalidCastException cast, raised when an explicit conversion fails
MulticastNotSupportedException Multicast not supported: Raised when two non-null delegates failed
The method called by NotSupportedException is not implemented in the class
NullReferenceException when referencing an empty reference object
OutOfMemoryException failed to allocate memory for new statement, not enough memory
OverflowException Overflow
StackOverflowException Stack Overflow
Initialization type for TypeInitializationException error: Thrown when a static constructor has a problem
NotFiniteNumberException

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.