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

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 夢在天涯 閱讀(746) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1816643
  • 排名 - 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>
              一区二区三区四区五区视频| 999亚洲国产精| 欧美一区二区三区免费在线看| 国产精品扒开腿爽爽爽视频| 午夜精品久久久久久99热| 亚洲欧美成人网| 国产在线不卡精品| 麻豆精品91| 欧美人与禽猛交乱配视频| 一区二区三区精品国产| 亚洲女同精品视频| 伊人蜜桃色噜噜激情综合| 欧美国产免费| 欧美特黄一级| 久久精品国产精品| 快射av在线播放一区| 中文av一区特黄| 性欧美videos另类喷潮| 亚洲日本国产| 亚洲男人影院| 亚洲国产精品黑人久久久| 99精品99| 在线成人激情| 99热这里只有成人精品国产| 国产日本欧美一区二区三区在线| 免费av成人在线| 国产精品a级| 欧美成人xxx| 欧美涩涩视频| 男人插女人欧美| 国产精品理论片在线观看| 欧美 日韩 国产一区二区在线视频 | 亚洲国产精彩中文乱码av在线播放| 亚洲精品123区| 国产精品一区久久久久| 欧美高清一区二区| 国产色产综合产在线视频| 最新国产成人在线观看| 狠狠久久综合婷婷不卡| 在线视频中文亚洲| 91久久精品久久国产性色也91| 亚洲一二区在线| 一本色道久久综合亚洲二区三区| 欧美一区二区免费| 午夜视频在线观看一区二区| 欧美精品一区二区高清在线观看| 美女免费视频一区| 国产日韩在线播放| 亚洲永久免费| 亚洲在线一区| 欧美日韩精品在线观看| 亚洲国产成人在线播放| 在线观看国产成人av片| 欧美一级在线播放| 久久精品理论片| 国产精品日韩精品欧美在线| 日韩午夜精品视频| 在线视频精品一| 欧美顶级大胆免费视频| 亚洲国产精品第一区二区三区| 韩国自拍一区| 久久久久久欧美| 免费不卡欧美自拍视频| 亚洲福利在线视频| 麻豆精品91| 亚洲国产日韩在线| 99国产精品国产精品毛片| 欧美精品久久久久久| 亚洲国产精品久久久| 亚洲七七久久综合桃花剧情介绍| 欧美成人免费全部| 亚洲日本va午夜在线电影| 日韩网站在线观看| 欧美日韩天天操| 亚洲综合精品| 久久久精品性| 91久久久在线| 欧美日韩国产va另类| 一区二区三区回区在观看免费视频| 亚洲一区二区三区四区五区午夜 | 国产精品分类| 亚洲欧美美女| 另类亚洲自拍| 日韩天天综合| 国产精品一香蕉国产线看观看 | 亚洲在线观看视频网站| 久久精品三级| 亚洲国产免费看| 欧美日韩中文字幕精品| 午夜精品一区二区三区四区 | 久久精品一区二区三区中文字幕| 伊人久久综合| 欧美三级在线| 久久久国产一区二区三区| 亚洲欧洲另类国产综合| 欧美一区二区三区视频在线观看| 国内外成人免费激情在线视频| 米奇777超碰欧美日韩亚洲| 亚洲精品一区二区三区在线观看| 欧美一区成人| 亚洲美女在线国产| 国产视频一区免费看| 欧美大片在线看| 亚洲综合久久久久| 91久久精品美女| 久久av一区二区三区亚洲| 亚洲精品免费一二三区| 国产农村妇女精品| 欧美日韩高清区| 久久久久久夜| 亚洲欧美日韩精品在线| 亚洲欧洲在线播放| 久久夜色精品国产亚洲aⅴ| 亚洲视频在线观看网站| 亚洲福利视频二区| 国产一区二区三区久久久| 欧美日韩国产小视频在线观看| 久久精品国产亚洲精品| 亚洲伊人久久综合| 亚洲毛片视频| 亚洲电影有码| 能在线观看的日韩av| 久久久999成人| 亚洲欧美另类中文字幕| 一级日韩一区在线观看| 在线欧美福利| 尤物视频一区二区| 国产欧美日韩精品a在线观看| 欧美日本高清| 欧美乱大交xxxxx| 欧美国产日韩一区| 欧美成人免费大片| 狼狼综合久久久久综合网| 欧美一区二区三区成人| 亚洲欧美另类在线观看| 亚洲自拍偷拍色片视频| 亚洲少妇一区| 亚洲中午字幕| 性色一区二区三区| 亚洲欧美综合v| 性色一区二区| 久久成人资源| 久久久蜜臀国产一区二区| 久久精品人人| 久久综合久久美利坚合众国| 久久性天堂网| 欧美大片免费久久精品三p | 国产情人综合久久777777| 国产精品嫩草久久久久| 国产精品久久久久久久久果冻传媒| 欧美日韩亚洲一区在线观看| 欧美午夜在线一二页| 国产精品久线观看视频| 国产欧美日韩免费看aⅴ视频| 国产精品夜夜夜一区二区三区尤| 国产精品亚洲视频| 韩国亚洲精品| 91久久精品国产91久久性色| 99re在线精品| 亚洲一级片在线观看| 欧美亚洲视频在线观看| 久久久蜜臀国产一区二区| 欧美freesex8一10精品| 亚洲国产日韩一区二区| 99精品国产99久久久久久福利| 一区二区三区高清视频在线观看| 亚洲一区二区少妇| 久久久噜噜噜久久狠狠50岁| 欧美1区2区| 国产精品九九| 海角社区69精品视频| 亚洲欧洲在线播放| 亚洲欧美日韩视频二区| 久久久亚洲影院你懂的| 亚洲激情亚洲| 亚洲欧美日韩在线一区| 麻豆九一精品爱看视频在线观看免费| 欧美va天堂| 国产精品视频第一区| 亚洲电影在线| 亚洲中字黄色| 美日韩精品视频免费看| 一区二区三区导航| 久久亚洲视频| 国产精品综合av一区二区国产馆| 在线日韩欧美| 午夜精品久久久久久久99水蜜桃| 欧美电影免费网站| 亚洲欧美日韩精品久久亚洲区| 麻豆精品视频在线观看| 国产精品永久入口久久久| 亚洲第一区在线| 欧美中文字幕久久| 日韩视频在线免费观看| 久久综合久久综合久久综合| 国产精品一二三视频| 一区二区三区.www| 欧美a一区二区| 欧美一区永久视频免费观看| 欧美视频在线观看|