C # debugging and error handling

Then good programmer in the programming process will also make mistakes, not to mention those of us junior scholars program; good programmers will find some mistakes and modify them some subtle mistakes sometimes there is a program we can not be found. when you have the tracking code for debugging.

A .Visual Studio Debugging

When you run the program in debug mode, operation is to write good code. Information debugger each line of code that took place during what has been recorded in the VS, the information stored in .pdb format files in the Debug directory.

During operation the text written in the Output window is very simple at this time can use about two commands:

Debug.WriteLine() 

Trace.WriteLine()

Both command function method is similar to the difference in the first command is run in debug mode while the second can also be used to publish the program refers to these two functions would also like to join 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]; // breakpoint design

index = i;

}

}

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

return max;

Actually, the problem may be related to debugging are themselves only specialized program running process to achieve those people would be concerned about the process to Debug, and in general we find that the program does not run when the error can not care about the specific implementation process.

II. Exception handling.

In the presence of abnormal program is running, for example, to determine whether there is to open the file in the open file, if it does not exist may cause abnormal causes the program to abort if there is an abnormality may be necessary to use some method of processing, in order to ensure the normal procedures run.

try

{

<Abnormality during the execution may contain>

}

catch (Exception catch the exception)

{

<Exception Processing>

}

finally {<eventually do>

}

The type of anomaly: a basic Exception.2: Application Exception class

3 exception classes related parameters: ArgumentException Class Format Exception categories:

4: member access and related exceptions MemberAccessException class that includes three seeds

FileAccessException categories: MethodAccessException categories: 
   MissingMemberException categories:

Examples down is common in data processing.

int x1 = 40;

int x2 = 0;

int x3;

Label re = new Label();

re.Text=Convert.ToString(x1/x2)

The results are

 

Try --- catch --- finally exception handling mode is performed.

Try intention to do what the program is, Catch is abnormal Try to capture the process that may arise, if there is an abnormal and unusual words and the same type of Catch, Catch an exception handling code .finally of whether or not an exception will be executed. Of course, there can be multiple Catch an exception to handle multiple processes capture program execution that may arise.

这里就不多说了.下面列举出C#处理过程中存在的不同异常类型.,
MemberAccessException                           访问错误:类型成员不能被访问   
ArgumentException                               参数错误:方法的参数无效   
ArgumentNullException                           参数为空:给方法传递一个不可接受的空参数   
ArithmeticException                             学计算错误:由于数学运算导致的异常,覆盖面广。   
   ArrayTypeMismatchException                      数组类型不匹配   
    
DivideByZeroException                           被零除   
    FormatException                                 参数的格式不正确   
    IndexOutOfRangeException               索引超出范围,小于0或比最后一个元素的索引还大   
    InvalidCastException                            法强制转换,在显式转换失败时引发   
    MulticastNotSupportedException                  不支持的组播:组合两个非空委派失败时引发   
    NotSupportedException                           调用的方法在类中没有实现   
    NullReferenceException                          引用空引用对象时引发   
    OutOfMemoryException                            无法为新语句分配内存时引发,内存不足   
    OverflowException                               溢出   
    StackOverflowException                          栈溢出   
    TypeInitializationException                     错误的初始化类型:静态构造函数有问题时引发   
    NotFiniteNumberException

转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/09/02/2667230.html

Guess you like

Origin blog.csdn.net/weixin_34335458/article/details/93495249