C # debugging and error handling

Source: Internet
Author: User

Good programmers will make some mistakes in programming. What's more, we are beginner programmers. Good programmers will find some mistakes and modify them. sometimes there are some subtle errors in the program. When we cannot find them, we have to track the code for debugging.

1. debugging in Visual Studio

When running a program in debug mode, the code is compiled. information about the debugging program is recorded during the execution of each line of code in vs. The information is stored in the DEBUG directory. in a PDB file.

Writing text to the output window during running is very simple. You can use the following two commands:

Debug. writeline ()

Trace. writeline ()

The usage of these two command functions is similar. The difference is that the first command is run in debug mode, and the second command can be used to publish programs. to reference these two functions, you must add using system to the namespace. diagnostics;

Static int maxnum (INT [] )//

{

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]; // breakpoint Design

Index = I;

}

}

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

Return Max;

In fact, debugging problems can be solved by yourself. Only those who specialize in program running will pay attention to the debugging process, generally, we do not care about the specific execution process when we find that the program has no errors to run.

Ii. Exception Handling.

An exception exists during the program running. For example, when opening a file, you must determine whether the file exists before opening. If the file does not exist, an exception may occur and the program is aborted. if an exception may exist, use the following method to handle it ., to ensure the normal operation of the program.

Try

{

<Abnormal execution may be included>

}

Catch (exception capture exception)

{

<Exception Handling>

}

Finally {<what will be done in the end>

}

Exception type: 1 Basic exception.2: Application exception class

3. Parameter-related exception class: argumentexception format exception class:

4: memberaccessexception exception related to member access, which includes three subclasses

Fileaccessexception class: methodaccessexception class:
Missingmemberexception class:

The following examples are common in data processing.

Int X1 = 40;

Int X2 = 0;

Int X3;

Label Re = new label ();

Re. Text = convert. tostring (x1/x2)

The execution result is

 

Try --- catch --- finally's exception handling mode is executed in this way.

Try is what the program intends to do, catch is the exception that may occur during the catch try process, if there is an exception and the exception is the same as the catch type, catch handles the exception. code in finally will be executed no matter whether there is any exception. of course, multiple catch methods can be used to handle multiple exceptions that may occur during the execution of the capture program.

I will not talk about it here. The following lists the different exception types in the C # processing process .,
Memberaccessexception access error: type members cannot be accessed
Argumentexception parameter error: the method parameter is invalid.
The argumentnullexception parameter is null: an unacceptable null parameter is passed to the method.
Calculation Error of arithmeticexception: exceptions caused by mathematical operations have a wide coverage.
Arraytypemismatchexception array Type Mismatch

Dividebyzeroexception is divided by zero
The formatexception parameter format is incorrect.
The indexoutofrangeexception index is out of the range. It is smaller than 0 or greater than the index of the last element.
The invalidcastexception method is forced conversion, which is triggered when the explicit conversion fails.
Multicast not supported by multicastnotsupportedexception: triggered when two non-empty delegation fails to be combined
The method called by notsupportedexception is not implemented in the class.
Thrown when nullreferenceexception references an empty referenced object
Outofmemoryexception is caused when the memory cannot be allocated for the new statement, and the memory is insufficient.
Overflowexception Overflow
Stackoverflowexception Stack Overflow
Incorrect typeinitializationexception initialization type: caused by a problem with the static Constructor
Notfinitenumberexception

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.