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

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

搜索

  •  

積分與排名

  • 積分 - 1816645
  • 排名 - 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>
              91久久精品一区| 99精品国产福利在线观看免费 | 欧美一区二区久久久| 国产精品v片在线观看不卡| 中文一区二区| 亚洲女同在线| 激情五月***国产精品| 麻豆精品一区二区综合av| 久热这里只精品99re8久| 亚洲电影观看| 日韩西西人体444www| 国产精品久久久久影院亚瑟| 欧美主播一区二区三区| 欧美诱惑福利视频| 亚洲三级视频| 亚洲一级电影| 在线成人亚洲| 99精品国产一区二区青青牛奶| 国产精品久久久久免费a∨大胸| 久久国产高清| 欧美第一黄网免费网站| 亚洲综合国产| 久久免费的精品国产v∧| 99这里只有精品| 欧美怡红院视频| 99re6热在线精品视频播放速度| 国产精品99久久久久久宅男| 一区二区视频免费完整版观看| 亚洲美女在线视频| 国产一区二区成人久久免费影院| 欧美激情精品久久久久久黑人| 国产精品久久久久久久久借妻| 另类激情亚洲| 国产精品入口麻豆原神| 欧美黄色一级视频| 国产一本一道久久香蕉| 亚洲国产一区二区三区在线播 | 国产女人aaa级久久久级| 免费看成人av| 国产精品热久久久久夜色精品三区| 欧美福利视频在线观看| 国产日韩一区二区三区在线| 91久久精品国产91久久性色tv| 国产亚洲精品aa| 一区二区三区鲁丝不卡| 亚洲日本成人网| 久久久久久久久岛国免费| 亚洲欧美电影在线观看| 欧美大片网址| 欧美国产大片| 1024成人网色www| 性欧美办公室18xxxxhd| 亚洲欧美国产日韩中文字幕| 欧美日本亚洲| 亚洲风情在线资源站| 黄色日韩网站| 欧美在线观看一区| 久久aⅴ国产欧美74aaa| 国产精品五月天| 在线视频你懂得一区| 在线亚洲一区| 欧美久久一级| 亚洲三级视频| 一本大道久久a久久精二百| 欧美大片在线影院| 欧美成人综合在线| 亚洲国产精品电影| 免费久久99精品国产自| 男人插女人欧美| 亚洲丰满少妇videoshd| 久久综合久久久久88| 欧美大片一区二区三区| 91久久久亚洲精品| 欧美激情亚洲另类| 亚洲欧洲一区二区三区久久| 夜夜嗨av一区二区三区中文字幕 | 亚洲高清视频在线观看| 蜜臀av性久久久久蜜臀aⅴ四虎| 久久中文久久字幕| 亚洲福利国产精品| 欧美福利一区二区三区| 最新国产成人av网站网址麻豆 | 国产一区二区三区免费不卡| 欧美一区二区三区四区高清| 久久久999国产| 狠狠色伊人亚洲综合成人| 久久五月婷婷丁香社区| 亚洲第一精品影视| 亚洲视频你懂的| 国产欧美日韩一区二区三区在线 | 欧美诱惑福利视频| 欧美成人免费网站| 一本色道久久综合精品竹菊 | 国产专区一区| 牛牛影视久久网| 一区二区免费在线播放| 久久岛国电影| 亚洲精品视频免费| 国产精品毛片a∨一区二区三区|国| 欧美在线观看你懂的| 亚洲电影免费观看高清| 亚洲欧美日韩精品久久久久| 伊人男人综合视频网| 欧美日韩视频专区在线播放| 欧美一区91| 日韩午夜视频在线观看| 开心色5月久久精品| 夜夜嗨av一区二区三区网站四季av | 老司机成人网| 亚洲一级在线观看| 亚洲国产91| 国产精品久久久久久久久婷婷| 久久人人爽人人爽| 亚洲特级片在线| 蜜桃av一区二区在线观看| 亚洲网站视频福利| 亚洲高清二区| 国产自产高清不卡| 国产精品va| 欧美精品一区二区三区高清aⅴ| 午夜精品福利电影| 一区二区成人精品| 欧美国产精品劲爆| 久久久久一区二区三区| 亚洲一区二区成人| 亚洲伦理在线观看| 亚洲国产高清aⅴ视频| 国产日产欧美a一级在线| 欧美日韩在线免费| 欧美大片在线观看| 六月天综合网| 久久久久久久国产| 先锋影音国产一区| 亚洲尤物视频网| 一区二区三区四区五区精品视频 | 亚洲国产高清自拍| 久久综合九色综合欧美狠狠| 午夜在线播放视频欧美| 亚洲欧美福利一区二区| 亚洲视频免费看| 亚洲视频一区二区在线观看| 亚洲精品欧美在线| 亚洲精品中文字幕女同| 亚洲国产日韩欧美| 伊人久久婷婷色综合98网| 黄色av一区| 激情欧美一区二区三区| 激情久久影院| 在线成人激情视频| 亚洲韩日在线| 亚洲精品一区二区三区av| 亚洲精品视频二区| 99视频有精品| 亚洲午夜一区二区三区| 亚洲欧美欧美一区二区三区| 午夜精品一区二区三区在线| 午夜精品亚洲一区二区三区嫩草| 欧美一区二区三区播放老司机 | 亚洲视频电影在线| 亚洲欧美精品一区| 久久精品国产视频| 久久亚洲高清| 亚洲丁香婷深爱综合| 亚洲精品少妇30p| 亚洲视频观看| 久久久国产成人精品| 久久综合中文| 欧美日韩一区二区三区在线看 | 国产精品影音先锋| 精品av久久久久电影| 亚洲国产裸拍裸体视频在线观看乱了中文 | 国产精品丝袜91| 国产一二精品视频| 亚洲人成网站777色婷婷| 亚洲一卡久久| 久久色中文字幕| 亚洲开发第一视频在线播放| 亚洲一区综合| 免费亚洲电影在线| 国产精品v片在线观看不卡| 国产一区香蕉久久| 亚洲精品一区二区网址| 午夜精品久久久| 欧美不卡福利| 亚洲在线电影| 欧美韩日精品| 曰韩精品一区二区| 亚洲欧美另类久久久精品2019| 麻豆九一精品爱看视频在线观看免费 | 99精品国产99久久久久久福利| 羞羞色国产精品| 亚洲国产另类久久精品| 亚洲女同同性videoxma| 欧美激情一区二区三区成人| 国产在线一区二区三区四区| 亚洲一区二区三区四区中文 | 欧美在线视频在线播放完整版免费观看 | 亚洲精品小视频在线观看| 久久精彩视频| 国产精品久久久久9999|