程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#的throw異常處理語句

C#的throw異常處理語句

編輯:關於C#

throw語句用於發出在程序執行期間出現反常情況(異常)的信號。throw語句通常與try-catch或try-finally語句一起使用。可以使用throw語句顯式引發異常(這裡引發自定義異常)。創建用戶自定義異常,好的編碼方法是以“Exception”作為用戶自定義異常類名的結尾。

示例 throw語句的使用

本示例通過Exception派生了一個新異常類UserEmployeeException,該類中定義了3個構造函數,每個構造函數使用不同的參數,然後再拋出自定義異常。程序代碼如下。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ClsUserExecption
{
class Program
{
public class UserEmployeeException: Exception
{
private string errorinfo=string.Empty;
public UserEmployeeException()
{
}
public UserEmployeeException (string message) : base(message)
{
errorinfo = message;
}

public UserEmployeeException (string message, Exception
inner):base(message,inner)
{
errorinfo = message;
inner = null;
}
}
public static void Main()
{
try
{
throw new UserEmployeeException("error Info of use ");
}
catch (UserEmployeeException ey)
{
Console.WriteLine("輸出結果為:");
Console.WriteLine(ey.Message,ey.InnerException);
Console.Read();
}
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved