青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Exception Handling in C#

 Exception Handling in C#

In this article, you will learn about the usage of various exception handling statements in C# with the help of relevant listings.

Trapping and handling of runtime errors is one of the most crucial tasks ahead of any programmer. But, before discussing runtime errors, let's look at compile time errors, which are errors that occur during compilation time. They may be due to bad coding, misspelling of syntaxes, and so on. They can be corrected by studying the error messages and warnings the compiler produces.

On the other hand, runtime errors occur at the time the program executes and can't be corrected. A programmer can, however, take preventive measures while coding the program. To do so, a programmer should first identify these two aspects:

  • Find out the part or parts of a program that are most likely to emit runtime errors.
  • Handle those errors according to language conventions.

C# provides an elegant way to handle runtime errors with the help of the try, catch, and finally keywords. Before moving ahead, let's consider a situation where the exception is not handled. We will explain the concept with the help of a "Division by Zero" example. Listing 1 illustrates how to write a simple program without handling an exception:

Listing 1

class  Without
{
  public static void Main()
  {
    int x = 5;
    int y = 0;
    int z = x/y;
    Console.WriteLine(z);
  }
}

When you execute this program, the C# interpreter produces a series of error messages (see Figure 1) to notify you that a runtime error has occurred.



Click here for a larger image.

Figure 1—Exception Occurred

To avoid these error messages and provide the user with a user-friendly error message, you should enclose the error-prone areas in Listing 1 with the try-catch clause, as shown in Listing 2:

Listing 2

class  With
{
  public static void Main()
  {
    try
    {
      int x = 5;
      int y = 0;
      int z = x/y;
      Console.WriteLine(z);
    }
    catch(DivideByZeroException e)
    {
      Console.WriteLine("Error occurred, unable to compute");
    }
  }
}

When you execute the program in Listing 2, instead of error messages as in Figure 1, the statement inside the catch block (see Figure 2) would be printed as our output.



Click here for a larger image.

Figure 2—Exception Handled Successfully

DivideByZeroException is one of the built-in exceptions in C#, defined in the System namespace. There are lots of other exceptions, which we will discuss shortly. The base class of all Exception classes in the .NET Framework is System.Exception. Even though the System namespace defines lot of exceptions, some of them are specified in specialized namespaces such as System.Data, System.IO, and so forth. Table 1 describes some of the built-in exceptions defined in the System namespace.

Name Description of Probable Causes
ArithmeticException Data Type not matched, invalid casting, etc.
DivideByZeroException An attempt to divide a value by zero
FormatException Incorrect arguments for the methods
MissingMethodException An attempt to call an invalid method
OutOfMemoryException Not enough memory to complete an operation
OverflowException An attempt to give large values to a variable of some type

Table 1—Built-in Exceptions

In Listing 2, we have analyzed DivideByZeroException and the way to handle the same. There are many more examples, which you may try on your own.

Using the finally Clause

In the preceding listings, the statements inside the catch block would be executed only if an error occurs. If you substitute the value of y with 1 and execute, there will not be an exception. However, there may be some cases where you want something to be printed, whether the exception had occurred or not. You can do so by using the finally clause. Listing 3 illustrates this concept by revisiting Listing 2:

Listing 3

class  Withfinally
{
  public static void Main()
  {
    try
    {
      int x = 5;
      int y = 0;
      int z = x/y;
      Console.WriteLine(z);
    }
    catch(DivideByZeroException e)
    {
      Console.WriteLine("Error occurred, unable to compute");
    }
    finally
    {
      Console.WriteLine("Thank you for using the program");
    }
  }
}

In the Listing 3 code, the statement inside the finally block will always print as shown in Figure 3.



Click here for a larger image.

Figure 3—Using the finally Clause

Using the Message Property

In certain cases, you may have to print the built-in C# error messages when a runtime error occurs. The System.Exception namespace defines predefined error messages for every exception. To use them, you have to use the Message property found in the System.Exception namespace. Listing 4 illustrates the working of this property.

Listing 4

class WithMessage
{
  public static void Main()
  {
    try
    {
      int x = 5;
      int y = 0;
      int z = x/y;
      Console.WriteLine(z);
    }
    catch(DivideByZeroException e)
    {
      Console.WriteLine("Error occurred " +e.Message);
    }
  }
}
-------------------------------------------------------------------

About the Author

Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.

posted on 2005-11-18 12:03 夢在天涯 閱讀(734) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811718
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              久久成人精品一区二区三区| 在线一区视频| 巨乳诱惑日韩免费av| 国外成人免费视频| 久久久久久91香蕉国产| 久久九九有精品国产23| 亚洲激情电影在线| 日韩视频免费观看| 国产日韩欧美视频| 欧美顶级艳妇交换群宴| 欧美日产在线观看| 久久精品女人| 免费一区二区三区| 亚洲欧美日韩另类| 久久久久国产精品午夜一区| 99综合精品| 欧美一区二区三区视频免费播放| 18成人免费观看视频| 亚洲美女在线国产| 国内精品亚洲| 一本色道精品久久一区二区三区| 国产一区二区三区四区在线观看 | 国产精品毛片大码女人| 久久久久久电影| 欧美另类在线观看| 久久青草久久| 国产精品嫩草99av在线| 欧美国产视频日韩| 国产喷白浆一区二区三区| 亚洲第一色在线| 国产精品裸体一区二区三区| 欧美承认网站| 国产欧美日韩91| 亚洲区欧美区| 永久久久久久| 午夜精品av| 亚洲视频第一页| 免费不卡欧美自拍视频| 欧美在线视频一区二区三区| 欧美日本中文字幕| 欧美激情一区二区三区四区| 国产欧美一区二区三区久久人妖| 亚洲人成在线观看网站高清| 一色屋精品亚洲香蕉网站| 亚洲一区久久久| 亚洲图片欧洲图片日韩av| 卡一卡二国产精品| 久久精品日产第一区二区| 国产精品成人观看视频免费| 亚洲国产欧美日韩另类综合| 在线色欧美三级视频| 欧美一区免费| 先锋a资源在线看亚洲| 欧美日韩一区二区在线观看视频| 欧美成人午夜免费视在线看片 | 六月丁香综合| 久久婷婷蜜乳一本欲蜜臀| 国产精品综合| 亚洲影视九九影院在线观看| 亚洲一区免费网站| 欧美日韩一区二区免费在线观看 | 日韩视频三区| 夜夜嗨av一区二区三区网页| 欧美激情视频给我| 91久久精品美女| 99国内精品久久| 欧美激情一区二区三区在线视频| 亚洲国产99| 一区二区精品国产| 欧美视频在线观看视频极品| 亚洲视频精品在线| 欧美一区二区免费观在线| 国产欧美日韩视频一区二区三区| 午夜一级在线看亚洲| 久久久久国产精品一区二区| 一区二区三区在线看| 美女黄毛**国产精品啪啪| 亚洲国产精品一区| 中文在线资源观看网站视频免费不卡| 欧美精品一线| 亚洲网站在线播放| 久久久久久电影| 亚洲国产精品一区二区三区| 欧美风情在线观看| 中文高清一区| 久久久久久亚洲精品杨幂换脸| 一区二区在线观看视频| 欧美成人网在线| 亚洲一区二区在线播放| 久久夜色精品国产欧美乱极品| 亚洲国语精品自产拍在线观看| 欧美精品一区二区三区蜜桃| 亚洲女人天堂av| 欧美成人免费小视频| 亚洲一区视频| 在线精品一区| 欧美系列亚洲系列| 久久久久久久性| 99精品福利视频| 久久夜色精品| 亚洲一区尤物| 亚洲国产成人91精品 | 久久国产福利国产秒拍| 91久久在线| 久久精品在线播放| 日韩视频在线观看国产| 国产伦理精品不卡| 欧美韩日一区二区| 久久精品国产亚洲aⅴ| 日韩一区二区福利| 欧美aⅴ99久久黑人专区| 亚洲女人天堂av| 亚洲欧洲免费视频| 国产在线观看精品一区二区三区| 欧美日韩精品免费观看视频完整| 久久精品国产91精品亚洲| 亚洲午夜精品久久久久久app| 欧美高清自拍一区| 久久综合精品国产一区二区三区| 亚洲欧美日韩综合一区| 亚洲精品中文字| 在线观看国产精品淫| 国产日韩一级二级三级| 国产精品老女人精品视频| 欧美激情精品久久久久久| 久久在线视频| 久久精品国产99国产精品| 亚洲影院在线| 亚洲视频一起| 一区二区电影免费观看| 亚洲精品字幕| 亚洲精品中文在线| 亚洲日本欧美天堂| 亚洲经典一区| 最新国产精品拍自在线播放| 亚洲第一区中文99精品| 欧美激情精品久久久久久免费印度 | 国产精品美女久久久免费| 欧美日本一区| 欧美日韩视频一区二区三区| 欧美精品激情blacked18| 欧美高清在线视频观看不卡| 免费成人在线观看视频| 欧美成人午夜免费视在线看片| 久久中文精品| 欧美国产成人精品| 欧美激情综合五月色丁香| 欧美精品一区二区三区久久久竹菊| 欧美高清在线| 欧美日韩在线一区二区| 国产精品久久久久一区二区三区共 | 国产伦精品一区二区三| 国产精品网曝门| 国产日产欧产精品推荐色| 国产日韩欧美亚洲一区| 国内精品一区二区| 亚洲国产精品日韩| 亚洲美女黄色| 亚洲男人的天堂在线aⅴ视频| 欧美一区二区视频在线观看2020| 久久午夜国产精品| 欧美激情影音先锋| 一区二区高清| 欧美中日韩免费视频| 欧美aa在线视频| 欧美性色综合| 极品尤物久久久av免费看| 亚洲国产福利在线| 亚洲性线免费观看视频成熟| 久久精品国产91精品亚洲| 免费在线成人| 一区二区三区不卡视频在线观看 | 欧美一区二区三区四区在线观看地址 | 亚洲综合色网站| 久久久人成影片一区二区三区观看 | 久久久999精品免费| 欧美sm极限捆绑bd| 国产精品久久久久一区| 在线免费观看成人网| 亚洲视频一区在线观看| 久久久久成人精品免费播放动漫| 欧美电影在线观看| 亚洲一区精彩视频| 久久综合亚州| 国产精品久久波多野结衣| 亚洲第一网站免费视频| 午夜精品久久久久久久久久久久久 | 欧美一区2区三区4区公司二百| 免费在线一区二区| 亚洲影音一区| 欧美日本一道本| 亚洲国产精品va| 久久av一区二区三区| 日韩视频一区二区在线观看 | 99在线精品免费视频九九视| 久久亚洲欧美| 国产一区美女| 欧美一区二区网站| 一区二区高清视频| 欧美区亚洲区|