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

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久久久久久久久| 夜久久久久久| 国产综合久久| 亚洲日韩第九十九页| 欧美日韩精品一区视频| 亚洲欧美在线另类| 久久国产精品毛片| 亚洲免费久久| 亚洲欧美日韩在线播放| 在线观看日韩精品| 一本色道久久综合狠狠躁篇怎么玩 | 久热国产精品| 一区二区三区波多野结衣在线观看| 9色国产精品| 激情久久综合| 一本大道久久a久久综合婷婷| 国产乱理伦片在线观看夜一区| 美女被久久久| 欧美午夜精品久久久久免费视| 久久久久久网址| 欧美日韩精品一区二区天天拍小说| 香蕉免费一区二区三区在线观看| 久久久视频精品| 亚洲综合好骚| 欧美大片91| 老司机67194精品线观看| 欧美日韩精品免费观看视频完整| 欧美一级大片在线观看| 欧美黄色大片网站| 久久影院午夜论| 国产精品一级久久久| 91久久夜色精品国产九色| 国产亚洲一区二区三区在线观看 | 亚洲一区二区成人| 亚洲人体影院| 久久久www成人免费毛片麻豆| 亚洲在线免费视频| 欧美精品播放| 嫩草国产精品入口| 海角社区69精品视频| 亚洲视频大全| 亚洲午夜精品久久| 欧美日韩国产免费| 欧美激情中文字幕乱码免费| 国产色综合久久| 亚洲欧美国产精品桃花| 一区二区三区色| 欧美成熟视频| 亚洲国产成人精品女人久久久 | 欧美小视频在线观看| 欧美国产高潮xxxx1819| 伊人久久大香线蕉av超碰演员| 亚洲男女自偷自拍| 羞羞漫画18久久大片| 国产精品国产三级国产专播品爱网| 欧美激情免费观看| 亚洲人成人77777线观看| 久久综合电影一区| 欧美福利电影网| 亚洲欧洲一区| 欧美91大片| 亚洲黄色大片| 99在线视频精品| 欧美日韩999| 亚洲午夜久久久久久尤物| 亚洲一区二区日本| 国产精品色午夜在线观看| 亚洲在线观看视频网站| 久久精品国产亚洲精品 | 欧美日韩国产高清视频| 亚洲精品一级| 午夜精品一区二区三区四区| 国产精品网站在线观看| 欧美一区二区精美| 欧美黄色精品| 在线亚洲观看| 国产欧美日韩一区二区三区在线观看 | 亚洲日本电影在线| 欧美日韩一区二区三区四区在线观看| 亚洲免费黄色| 久久久久国产一区二区三区| 在线不卡欧美| 欧美日韩一区二区在线| 午夜精品影院| 欧美电影美腿模特1979在线看| 亚洲精品亚洲人成人网| 欧美色另类天堂2015| 亚洲欧美亚洲| 欧美黄污视频| 午夜精品一区二区三区在线视| 韩国三级电影久久久久久| 欧美www视频| 亚洲永久免费视频| 欧美黄色精品| 欧美一区免费视频| 亚洲欧洲日本专区| 国产精品一区二区黑丝| 欧美成人蜜桃| 欧美一区二区三区免费视频| 亚洲国产精品久久精品怡红院| 亚洲欧美视频一区二区三区| …久久精品99久久香蕉国产 | 蜜桃av噜噜一区| 亚洲视频精品| 亚洲国产精品福利| 久久综合久久久久88| 一区二区免费在线视频| 在线播放一区| 国产欧美精品va在线观看| 欧美激情国产高清| 久久综合精品国产一区二区三区| 一区二区三区 在线观看视频 | 午夜欧美精品| 一区二区成人精品| 亚洲春色另类小说| 国产午夜精品全部视频在线播放| 欧美日韩国产不卡| 欧美成人综合在线| 乱码第一页成人| 欧美在线观看网站| 午夜国产精品视频| 亚洲永久在线| 亚洲一区欧美| 一区二区三区欧美成人| 亚洲精品一区二区三区樱花 | 99re热精品| 亚洲国产精品久久久久婷婷884| 国产亚洲人成网站在线观看| 国产精品免费一区豆花| 欧美私人啪啪vps| 欧美日韩中文在线| 欧美色图麻豆| 欧美激情第五页| 欧美剧在线观看| 欧美高清视频一二三区| 欧美大尺度在线观看| 免费观看在线综合色| 美女任你摸久久| 欧美gay视频| 欧美韩日精品| 欧美日韩高清区| 国产精品久久9| 国产精品最新自拍| 国产日韩一区二区三区在线播放| 国产欧美一区二区精品秋霞影院| 国产精品久久久一区麻豆最新章节| 欧美无乱码久久久免费午夜一区 | 香蕉成人伊视频在线观看| 亚洲欧美中文另类| 欧美在线高清视频| 久久久精品欧美丰满| 美玉足脚交一区二区三区图片| 久久激情网站| 免费一区二区三区| 欧美日韩另类国产亚洲欧美一级| 欧美日韩国产精品一区二区亚洲| 欧美日韩成人一区二区| 国产精品久久福利| 国际精品欧美精品| 亚洲精选在线| 欧美一级欧美一级在线播放| 久久久久.com| 亚洲精品日韩在线| 在线观看日韩国产| 狠狠色狠狠色综合日日91app| 一区二区在线视频播放| 亚洲精品系列| 欧美一级片久久久久久久| 久久综合激情| 亚洲日本中文字幕免费在线不卡| 日韩西西人体444www| 久久福利精品| 欧美日韩精选| 影音先锋久久精品| 亚洲性线免费观看视频成熟| 久久久久九九视频| 日韩午夜在线视频| 久久精品亚洲一区| 欧美日韩一区二区三区视频| 韩日成人在线| 亚洲免费影视| 欧美福利视频在线| 亚洲专区一区| 欧美日韩国产综合新一区| 国产午夜精品理论片a级大结局| 亚洲日本欧美| 久久米奇亚洲| 亚洲一区二区三区高清不卡| 欧美jizz19hd性欧美| 国产一区二区精品久久99| 亚洲午夜电影| 亚洲黄色成人网| 蜜桃精品久久久久久久免费影院|