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

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>
              亚洲免费一级电影| 亚洲激情图片小说视频| 亚洲三级电影全部在线观看高清| 亚洲欧美影院| 欧美成人国产一区二区| 午夜欧美精品| 国产精品日日摸夜夜摸av| 亚洲裸体视频| 亚洲日韩成人| 欧美精品一区二区三区四区| 亚洲激情在线激情| 亚洲国产日韩欧美在线动漫| 久久综合99re88久久爱| 在线日韩成人| 欧美福利影院| 女女同性女同一区二区三区91| 影音先锋在线一区| 欧美电影免费观看大全| 性色av一区二区三区| 欧美专区中文字幕| 亚洲黄色性网站| 米奇777超碰欧美日韩亚洲| 一区二区在线视频播放| 久久人91精品久久久久久不卡| 欧美一区日本一区韩国一区| 欧美日本一区二区高清播放视频| 亚洲精品日本| 亚洲午夜黄色| 欧美午夜剧场| 亚洲视频电影图片偷拍一区| 亚洲一卡久久| 在线日韩欧美视频| 最新国产乱人伦偷精品免费网站| 欧美乱在线观看| 亚洲欧美成人一区二区三区| 亚洲男女自偷自拍| 亚洲欧美一级二级三级| 亚洲婷婷在线| 韩国成人理伦片免费播放| 欧美粗暴jizz性欧美20| 欧美日韩aaaaa| 性欧美大战久久久久久久免费观看 | 欧美一区二区高清| 久久久精品免费视频| 激情av一区| 亚洲人成网在线播放| 国产美女诱惑一区二区| 亚洲高清视频中文字幕| 国产精品va在线| 亚洲欧美文学| 欧美插天视频在线播放| 欧美一级成年大片在线观看| 久久欧美中文字幕| 午夜精品视频网站| 欧美国产精品人人做人人爱| 久久成人羞羞网站| 精品成人国产| 欧美高清视频免费观看| 国产精品成人aaaaa网站| 久久国产福利国产秒拍| 欧美国产精品va在线观看| 欧美一区亚洲| 欧美日本在线视频| 另类av一区二区| 国产精品美女久久久浪潮软件| 欧美一区二区三区婷婷月色| 欧美连裤袜在线视频| 久久这里只有| 国产欧美一区二区精品忘忧草| 欧美激情久久久久久| 国产亚洲一区在线播放| 亚洲一区二区在| 亚洲第一成人在线| 久久精品亚洲乱码伦伦中文| 小嫩嫩精品导航| 欧美日韩日本国产亚洲在线| 午夜精品福利一区二区蜜股av| 欧美大片免费久久精品三p| 久久嫩草精品久久久精品一 | 亚洲最新视频在线播放| 久久性色av| 蜜臀va亚洲va欧美va天堂 | 亚洲在线视频一区| 亚洲成色777777女色窝| 久久精品一区二区三区中文字幕| 亚洲欧美日韩系列| 国产精品爽爽爽| 亚洲小说欧美另类社区| 午夜精品影院| 国产日韩1区| 久久精品国产久精国产思思| 久久久精品国产免大香伊| 国产午夜精品麻豆| 欧美亚洲综合另类| 久久久久一区二区三区| 欧美午夜视频网站| 免费91麻豆精品国产自产在线观看| 亚洲国产黄色片| 亚洲日韩欧美一区二区在线| 老巨人导航500精品| 欧美高清视频免费观看| 亚洲老板91色精品久久| 欧美日韩精品在线| 亚洲国产综合在线看不卡| 亚洲免费成人av电影| 欧美日韩四区| 夜夜夜久久久| 亚洲神马久久| 国产精品免费看久久久香蕉| 午夜国产欧美理论在线播放| 久久久精品动漫| 亚洲国产精品成人综合| 欧美日韩国产限制| 日韩午夜av| 久久精品国产2020观看福利| aa国产精品| 欧美激情一区二区三区在线视频观看| 亚洲日本aⅴ片在线观看香蕉| 一区二区精品在线| 国产日韩av高清| 久久亚洲视频| 欧美一级淫片aaaaaaa视频| 亚洲国产毛片完整版| 久久久噜噜噜久噜久久| 亚洲伊人伊色伊影伊综合网| 亚洲国产精品久久久久秋霞影院| 国产精品久久一卡二卡| 欧美日本精品在线| 欧美.com| 另类激情亚洲| 久久久九九九九| 亚洲欧美影院| 亚洲特级毛片| 一本色道**综合亚洲精品蜜桃冫 | 国产精品久久久久久久久久ktv | 久久人人爽人人爽| 性色av一区二区三区红粉影视| 亚洲理论在线观看| 亚洲国产精品成人久久综合一区| 久久久亚洲影院你懂的| 欧美一区二区在线播放| 亚洲欧美精品伊人久久| 亚洲网站在线| 亚洲午夜精品在线| 亚洲色诱最新| 一区二区三区四区国产精品| 亚洲人成在线观看一区二区| 亚洲国产欧美日韩精品| 亚洲第一精品影视| 亚洲第一福利在线观看| 亚洲国产精品福利| 亚洲精品自在久久| 最近看过的日韩成人| 亚洲精品一区二区三区四区高清 | 欧美精品在线免费播放| 欧美成人精精品一区二区频| 欧美成人一品| 欧美精品日本| 欧美视频在线一区| 欧美午夜精品久久久| 国产精品国产三级国产aⅴ9色| 欧美婷婷久久| 国产午夜精品一区二区三区欧美| 国产日本欧美一区二区三区在线| 国产日本欧美视频| 亚洲电影免费观看高清完整版| 亚洲国产高清自拍| 亚洲伦理久久| 欧美亚洲综合网| 你懂的国产精品| 亚洲欧洲在线播放| 亚洲免费在线精品一区| 久久九九久精品国产免费直播| 久久综合导航| 国产精品v亚洲精品v日韩精品| 国产精品色午夜在线观看| 国产欧美日韩综合| 亚洲人成毛片在线播放| 亚洲女人天堂成人av在线| 久热国产精品视频| 日韩视频精品在线观看| 欧美一区2区三区4区公司二百| 久久免费观看视频| 欧美性一区二区| 在线观看免费视频综合| 亚洲一区二区三区免费视频| 久久人人爽人人爽| 亚洲另类视频| 久久久久.com| 欧美色精品天天在线观看视频 | 欧美日韩在线精品一区二区三区| 国产精品自拍网站| 亚洲另类春色国产| 久久色中文字幕| 日韩亚洲欧美一区| 久久综合图片| 国内揄拍国内精品少妇国语| 亚洲色图在线视频| 欧美激情1区2区3区| 午夜伦理片一区|