如何自定义异常类,并如何使用???
我定义了好久都没有出来!!
以下是我的代码,我新手,请指教
自定义异常类:
//自定义异常处理类
using System;
using System.Diagnostics;namespace MyAppException
{
/// <summary>
/// 从系统异常类ApplicationException继承的应用程序异常处理类。
/// 自动将异常内容记录到Windows NT/2000的应用程序日志
/// </summary>
public class AppException : System.ApplicationException
{
public AppException()
{ } public AppException(string message, Exception inner):base(message,inner) { }
public class DuplicateCustomerIDException : AppException
{ public DuplicateCustomerIDException()
{ }
public DuplicateCustomerIDException(string message, Exception inner)
: base(message, inner)
{ } } }前台的异常:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MyAppException.AppException ma = new MyAppException.AppException();
SqlConnection cn = new SqlConnection("server =.;database=Northwind;uid=sa;pwd=sa"); cn.Open();
try
{
SqlCommand com = new SqlCommand("Insert Into Customers (CustomerID,CompanyName,ContactName) Values ('" + ftxt_CustomerID.Text + "','" + ftxt_CompanyName.Text + "','" + ftxt_ContactName.Text + "')", cn); com.ExecuteNonQuery(); } catch (SqlException ex)
{ if (ex.Number == 2627)
{ throw new DuplicateCustomerIDException("CustomerID重复...", ex); } else
{ MessageBox.Show("成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } } finally
{ cn.Close(); }
}
}
}
我定义了好久都没有出来!!
以下是我的代码,我新手,请指教
自定义异常类:
//自定义异常处理类
using System;
using System.Diagnostics;namespace MyAppException
{
/// <summary>
/// 从系统异常类ApplicationException继承的应用程序异常处理类。
/// 自动将异常内容记录到Windows NT/2000的应用程序日志
/// </summary>
public class AppException : System.ApplicationException
{
public AppException()
{ } public AppException(string message, Exception inner):base(message,inner) { }
public class DuplicateCustomerIDException : AppException
{ public DuplicateCustomerIDException()
{ }
public DuplicateCustomerIDException(string message, Exception inner)
: base(message, inner)
{ } } }前台的异常:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MyAppException.AppException ma = new MyAppException.AppException();
SqlConnection cn = new SqlConnection("server =.;database=Northwind;uid=sa;pwd=sa"); cn.Open();
try
{
SqlCommand com = new SqlCommand("Insert Into Customers (CustomerID,CompanyName,ContactName) Values ('" + ftxt_CustomerID.Text + "','" + ftxt_CompanyName.Text + "','" + ftxt_ContactName.Text + "')", cn); com.ExecuteNonQuery(); } catch (SqlException ex)
{ if (ex.Number == 2627)
{ throw new DuplicateCustomerIDException("CustomerID重复...", ex); } else
{ MessageBox.Show("成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } } finally
{ cn.Close(); }
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货