A discussion on C # Error debugging and exception handling

Source: Internet
Author: User
Tags finally block throw exception

There will inevitably be a variety of errors in the program design, which need to be avoided as much as possible when writing code.

When dealing with errors, you should first parse the wrong

The wrong type, find out the cause of the error ability to resolve errors.


Classification of errors



watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemhhbmd6awppzwppyxlvdq==/font/5a6l5l2t/fontsize/400/fill/ I0jbqkfcma==/dissolve/70/gravity/center ">


Error handling


One, there are two types of error handling, such as the following:

1. Commissioning

Debugging we used frequently, what set breakpoint positioning error, and C # positioning error is very easy, also prompts the wrong type. This is not a specific point.

2. Add exception-handling statements to make the program fault tolerant

(1) C # Four keyword:try, catch, finally, throw to manage exceptions.

(2) Error handling statements



(A). Try....catch

Inside a try is a program code that can be abnormal, and the catch is the exception control code that handles the exception type.

In fact, it is almost identical to the go to statement in VB.

Eg: Catch integer except 0 error by Try...catch statement

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"  > Using System;  Using System.Collections.Generic;  Using System.Text;  Namespace error Handling {  class program   {  static void Main (string[] args)      {  int x = 5,y = 0;         try                                      //try...catch statement         {            x =x/y;                               Throw except 0 error         }         catch (Exception err)                     //Catch the error         {            Console.WriteLine ("{0}", err. Message); Display error message         }      }   }}</span>  

(B). Try...catch...finally

And the Try...catch statement is more than a finally block, its role is whether or not an exception occurs. Finally blocks will run. We're totally adding a finally block to the last 0 examples. Very easy.

(C). Throw

There are two ways to use it: one is to throw an exception, one is to handle an exception with a catch block when an exception occurs, and then another throws an exception using the throw statement

Eg: In the last addition to the 0 example, add throw such as the following

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"  > Using System;  Using System.Collections.Generic;  Using System.Text;  Namespace error Handling {  class program   {  static void fun ()      {  int x = 5,y = 0;         try                                      //try...catch statement         {            x =x/y;                               Throw except 0 error         }         catch (Exception err)                     //Catch the error         {            Console.WriteLine ("{0}", err. Message); Display error message            throw;                                Throw exception again         }      }      static void Main (string[] args)     {  try {fun        ()}        catch (Exception err)                     //Catch the error        {console.writline ("main:{}", Err.  Message;}     }   }} </span>  

Exception classes that are frequently used in C #




Summary

Programming is the process of continuous debugging, and then the program ape will not write completely error-free code, this blog post is our daily C #

A small summary of the avoidance and handling of errors in programming. http://blog.csdn.net/zhangzijiejiayou/article/details/9857153 This

is a link to the VB error processing, I hope to help you.




A discussion on C # Error debugging and exception handling

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.