C#的throw异常处理语句
2012-12-06 21:40
381 查看
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { public class UserEmployeeException : Exception { private string errorinfoSK = string.Empty; public UserEmployeeException() { } public UserEmployeeException(string message) : base(message) { errorinfoSK = message; } public UserEmployeeException(string message, Exception inner) : base(message, inner) { errorinfoSK = message; inner = null; } } public static void Main() { try { throw new UserEmployeeException("error Info of use "); } catch (UserEmployeeException ey) { Console.WriteLine(ey.Message); Console.WriteLine("输出结果为:"); Console.WriteLine(ey.InnerException); Console.Read(); } } } } /* error Info of use 输出结果为: */
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(); } } } }
相关文章推荐
- 【c#】异常处理:try catch throw
- 黑马程序员----C#中的流程控制语句之异常处理语句
- (js异常处理)Throw 语句
- 【又长见识了】C#异常处理,try、catch、finally、throw
- java异常处理-finally中使用return和throw语句
- C#2继承、foreach语句及异常处理语句
- C# 自定义异常类 throw语句抛出异常
- 天轰穿C# -vs2010 - 03C#的异常处理之Throw关键字【原创】
- C# 自定义异常类 throw语句抛出异常
- c#描述异常处理语句try、catch、finally执行时的相互关系
- 我的c#之路(8.异常处理语句)
- C# .NET编程基础-throw异常处理-Email格式检测示例
- C# 语句异常处理语句
- 22 C#中的异常处理入门 try catch throw
- C#中异常处理语句Finally
- C++中异常处理的基本思想及throw语句抛出异常的使用
- c#描述异常处理语句try、catch、finally执行时的相互关系
- C#异常在catch语句中默认处理
- 学习笔记之C#类、循环中的语句、基础知识和异常处理
- c#描述异常处理语句try、catch、finally执行时的相互关系