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

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>
              亚洲一区二区毛片| 久久久久久久久久码影片| 欧美激情第二页| 亚洲精品欧美日韩| 亚洲精品自在久久| 国产精品久久国产精品99gif| 亚洲最新视频在线| 亚洲美女淫视频| 国产精品国产a级| 欧美专区在线观看| 久久精品一级爱片| 亚洲精品久久久久久久久| 亚洲激情影院| 欧美日韩精品免费看| 香蕉亚洲视频| 久久尤物视频| 亚洲一区欧美激情| 久久成人精品| 亚洲精品一区中文| 亚洲自拍偷拍视频| 亚洲国产精品久久91精品| 亚洲欧洲三级| 国产日韩免费| 欧美大成色www永久网站婷| 欧美日韩成人激情| 久久久久久久久久久久久9999| 蜜臀av性久久久久蜜臀aⅴ四虎| 亚洲美女中出| 午夜国产精品视频| 99视频有精品| 久久亚洲图片| 午夜精品免费在线| 欧美大片在线看免费观看| 欧美一区二区三区在线播放| 久久综合色婷婷| 欧美一级大片在线免费观看| 欧美.www| 久久综合99re88久久爱| 欧美视频导航| 亚洲高清免费视频| 国外视频精品毛片| 一区二区三区产品免费精品久久75| 韩日欧美一区二区三区| 亚洲一区二区三区四区中文| 亚洲国产精品成人综合| 欧美在线观看视频在线| 亚洲综合视频一区| 欧美好吊妞视频| 久久裸体艺术| 国产日韩一区二区三区在线播放 | 国产最新精品精品你懂的| 91久久久在线| 亚洲黄色在线| 久久aⅴ国产欧美74aaa| 亚洲欧美三级伦理| 欧美日本一区二区三区| 欧美大胆成人| 在线观看欧美日韩国产| 久久国产一二区| 久久久福利视频| 国产麻豆日韩| 午夜精品福利视频| 欧美亚洲一级片| 国产精品系列在线播放| 一区二区国产精品| 亚洲综合第一| 国产精品久久久久久超碰| 99精品国产热久久91蜜凸| 亚洲精品1234| 欧美理论电影在线观看| 亚洲国产精品久久久久秋霞蜜臀| 亚洲国产精品传媒在线观看 | 亚洲精品孕妇| 99精品视频免费| 欧美午夜宅男影院| 亚洲一区欧美二区| 久久久99免费视频| 国产一区二区三区在线观看精品 | 国产精品伊人日日| 亚洲欧美制服中文字幕| 久久久久久亚洲精品不卡4k岛国| 国产一区二区福利| 久久久久久穴| 亚洲人成7777| 香蕉尹人综合在线观看| 国产一区二区0| 免费短视频成人日韩| 最新国产乱人伦偷精品免费网站| 在线视频亚洲| 国产日产欧美精品| 蜜桃久久av| 这里只有精品丝袜| 久久精品亚洲一区二区| 亚洲高清自拍| 欧美图区在线视频| 久久er精品视频| 亚洲人成在线播放| 午夜精品国产精品大乳美女| 激情成人中文字幕| 欧美日韩国产一中文字不卡| 亚洲综合精品自拍| 女生裸体视频一区二区三区 | 国产精品一卡| 久久综合中文字幕| 一本久久综合亚洲鲁鲁五月天| 久久国产一区二区| 亚洲另类在线视频| 国产一区二区三区电影在线观看| 欧美好吊妞视频| 久久黄色小说| 一区二区av在线| 欧美激情精品久久久久久黑人 | 久久成人精品一区二区三区| 亚洲福利视频一区| 国产精品三级视频| 免费日本视频一区| 欧美影片第一页| 一本色道综合亚洲| 亚洲福利精品| 久久久亚洲人| 欧美一区成人| 亚洲午夜视频在线| 亚洲狠狠丁香婷婷综合久久久| 国产精品亚洲第一区在线暖暖韩国| 欧美二区不卡| 久久久久久久尹人综合网亚洲| 亚洲图色在线| 99国产精品久久久久久久久久| 欧美本精品男人aⅴ天堂| 欧美一区二区三区在线播放| 一本色道综合亚洲| 91久久国产综合久久| 国产午夜精品理论片a级大结局| 欧美日韩视频在线一区二区观看视频| 久久综合久久综合久久综合| 欧美在线免费视屏| 午夜精品网站| 欧美一区二区精品| 亚洲欧美一区二区原创| 亚洲永久免费视频| 99re热这里只有精品免费视频| 欧美激情乱人伦| 另类成人小视频在线| 久久久久在线| 久久亚洲精品一区二区| 久久国产精品毛片| 久久久精品五月天| 久久人人97超碰精品888| 久久黄色小说| 久久久免费av| 美女免费视频一区| 免费亚洲网站| 亚洲人成毛片在线播放| 亚洲精品一二| 亚洲午夜一二三区视频| 亚洲性感美女99在线| 亚洲欧美日韩国产一区二区| 亚洲欧美日韩国产精品| 久久成人18免费网站| 久久综合久久综合久久综合| 欧美电影电视剧在线观看| 欧美精品乱人伦久久久久久| 欧美日韩福利视频| 国产乱理伦片在线观看夜一区| 国产欧美日韩麻豆91| 黄色精品一二区| aaa亚洲精品一二三区| 亚洲一区二区成人| 久久激情视频久久| 欧美+亚洲+精品+三区| 亚洲欧洲精品一区二区三区不卡| 99亚洲一区二区| 欧美专区一区二区三区| 欧美成人精品一区| 国产精品视频导航| 亚洲激情影院| 亚洲制服丝袜在线| 久久精品一区蜜桃臀影院| 欧美激情中文字幕乱码免费| 99精品国产一区二区青青牛奶| 午夜日韩激情| 欧美日韩第一页| 国产一区二区三区av电影 | 激情综合电影网| 99国产精品久久久| 久久精品三级| 亚洲精品一二三| 久久精品亚洲热| 国产精品都在这里| 亚洲国产精品黑人久久久 | 亚洲国产精品一区二区三区| 宅男在线国产精品| 老色鬼精品视频在线观看播放| 日韩一级成人av| 美女图片一区二区| 国产一区三区三区| 亚洲欧美春色| 亚洲精品久久久久久久久久久久| 羞羞视频在线观看欧美| 欧美色精品天天在线观看视频|