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

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久久久综合| 久久综合伊人77777麻豆| 欧美在线一区二区三区| 精品盗摄一区二区三区| 欧美韩国一区| 欧美日本一区| 欧美一区激情| 久久在线免费| 亚洲一区不卡| 欧美中文日韩| 日韩视频精品在线| 亚洲砖区区免费| 黄色亚洲精品| 日韩视频在线观看| 国产视频一区在线观看| 嫩模写真一区二区三区三州| 欧美激情乱人伦| 亚洲欧美视频一区二区三区| 性欧美video另类hd性玩具| 亚洲国产毛片完整版| 亚洲精品午夜| 国产精品免费看| 男女精品网站| 国产精品久久久久久亚洲调教| 欧美在线你懂的| 欧美~级网站不卡| 欧美一区二区日韩一区二区| 欧美1区免费| 欧美在线一级视频| 欧美日韩成人在线播放| 久久久亚洲国产天美传媒修理工 | 日韩亚洲一区二区| 国产一区二三区| 亚洲深爱激情| 亚洲伦伦在线| 欧美在线视频免费观看| 亚洲综合视频网| 久久综合国产精品台湾中文娱乐网| 亚洲愉拍自拍另类高清精品| 蜜桃久久av一区| 久久婷婷国产综合精品青草| 欧美午夜精品电影| 亚洲全部视频| 亚洲成人自拍视频| 欧美在线999| 欧美一区=区| 国产精品v日韩精品v欧美精品网站| 免费观看不卡av| 狠狠色噜噜狠狠狠狠色吗综合| 亚洲视频电影在线| 国产精品99久久久久久白浆小说| 免费高清在线视频一区·| 欧美在线不卡| 国产欧美日韩精品专区| 一区二区三区精品视频| 日韩一区二区久久| 欧美国产极速在线| 亚洲国产专区校园欧美| 亚洲人成欧美中文字幕| 美女任你摸久久| 欧美承认网站| 亚洲激情综合| 欧美丰满少妇xxxbbb| 欧美激情影音先锋| 亚洲精品午夜| 欧美日韩一区综合| 中文网丁香综合网| 欧美一区二区黄| 国产一区二区视频在线观看| 欧美在线不卡| 欧美成人激情视频免费观看| 亚洲国产美女| 欧美激情亚洲自拍| 亚洲日本激情| 欧美亚洲视频| 尤物在线精品| 欧美精品久久一区二区| 99re6这里只有精品视频在线观看| 亚洲视频狠狠| 国产午夜精品久久| 免费观看30秒视频久久| 亚洲免费不卡| 久久精品国产久精国产思思| 伊人色综合久久天天五月婷| 欧美激情亚洲另类| 亚洲亚洲精品在线观看| 久久久久久穴| 亚洲伦理自拍| 国产亚洲精品久久久| 另类天堂av| 一区二区三区不卡视频在线观看| 欧美一级大片在线观看| 在线播放中文一区| 欧美日韩亚洲综合| 欧美一区二区在线视频| 亚洲福利视频一区| 欧美影院视频| a4yy欧美一区二区三区| 国产日韩欧美制服另类| 免费观看欧美在线视频的网站| 在线亚洲伦理| 欧美成人午夜激情视频| 亚洲综合日韩在线| 亚洲高清一二三区| 国产精品一区在线播放| 欧美成人精品影院| 欧美亚洲网站| 在线一区二区三区做爰视频网站| 久久久午夜视频| 亚洲欧美精品在线观看| 亚洲国产一二三| 国产一区日韩一区| 欧美无乱码久久久免费午夜一区| 久久精品视频免费播放| 亚洲视频在线一区观看| 亚洲高清视频在线观看| 久久人人97超碰精品888| 亚洲视屏在线播放| 亚洲另类黄色| 亚洲国产精品ⅴa在线观看| 国产日韩精品一区二区| 欧美日韩亚洲一区| 欧美激情91| 美国三级日本三级久久99| 欧美一区二区三区四区夜夜大片 | 免费人成网站在线观看欧美高清| 亚洲自拍偷拍色片视频| 日韩视频免费观看高清完整版| 国产亚洲福利社区一区| 国产精品揄拍一区二区| 欧美偷拍一区二区| 欧美日韩在线播| 欧美日韩免费观看一区二区三区 | 老司机免费视频一区二区| 欧美一区二区三区在线视频 | 亚洲网站视频| 亚洲天堂成人| 正在播放欧美一区| 一区二区三区精密机械公司| 亚洲三级免费| 99精品欧美一区二区三区综合在线 | 国内精品视频久久| 国产亚洲福利社区一区| 国产丝袜美腿一区二区三区| 国产农村妇女精品一二区| 国产精品一国产精品k频道56| 欧美亚男人的天堂| 国产伦精品一区二区三| 国产日韩欧美中文| 狠狠色伊人亚洲综合网站色| 在线精品亚洲| 99re6这里只有精品| 99视频精品全国免费| 亚洲在线中文字幕| 久久激情综合网| 欧美电影免费观看高清| 亚洲国产精品一区| av成人免费在线| 西西裸体人体做爰大胆久久久| 欧美在线视频全部完| 女生裸体视频一区二区三区| 欧美精品偷拍| 国产手机视频精品| 亚洲第一主播视频| 一二美女精品欧洲| 久久福利一区| 亚洲第一精品电影| 制服丝袜激情欧洲亚洲| 午夜激情综合网| 美女精品网站| 国产美女精品视频| 亚洲国产成人精品久久| 亚洲视频每日更新| 乱码第一页成人| 亚洲最新合集| 久久婷婷国产综合尤物精品| 欧美三级黄美女| 精品96久久久久久中文字幕无| 99国产精品久久久久老师| 欧美中文在线观看国产| 91久久综合亚洲鲁鲁五月天| 亚洲专区一区| 欧美精品一区视频| 在线观看日韩av| 午夜国产一区| 日韩视频免费在线观看| 久久久久久夜精品精品免费| 欧美色视频一区| 亚洲激情成人在线| 欧美在线亚洲在线| 日韩视频免费大全中文字幕| 亚洲毛片视频| 久久免费视频观看| 亚洲免费视频在线观看| 欧美日韩亚洲一区二区三区在线观看| 国产日韩欧美黄色| 亚洲欧美一区二区在线观看|