如何:在 Catch 块中使用特定异常

发生异常时,异常沿堆栈向上传递,每个 Catch 块都有机会处理它。 Catch 语句的顺序很重要。 将针对特定异常的 Catch 块放在常规异常 Catch 块的前面,否则编译器可能会发出错误。 确定正确 Catch 块的方法是将异常的类型与 Catch 块中指定的异常名称进行匹配。 如果没有特定的 Catch 块,则由可能存在的常规 Catch 块捕捉异常。

下面的代码示例使用 try/catch 块捕获 InvalidCastException。 该示例创建一个名为 Employee 的类,它带有一个属性:职员级别 (Emlevel)。 PromoteEmployee 方法取得对象并增加职员级别。 将 DateTime 实例传递给 PromoteEmployee 方法时,发生 InvalidCastException

示例

Imports System

Public Class Employee
    'Create employee level property.
    Public Property Emlevel As Integer
        Get
            Return emlevelValue
        End Get
        Set
            emlevelValue = Value
        End Set
    End Property

    Private emlevelValue As Integer = 0
End Class

Public Class Ex13
    Public Shared Sub PromoteEmployee(emp As Object)
        'Cast object to Employee.
        Dim e As Employee = CType(emp, Employee)
        ' Increment employee level.
        e.Emlevel = e.Emlevel + 1
    End Sub

    Public Shared Sub Main()
        Try
            Dim o As Object = New Employee()
            Dim newyears As New DateTime(2001, 1, 1)
            'Promote the new employee.
            PromoteEmployee(o)
            'Promote DateTime; results in InvalidCastException as newyears is not an employee instance.
            PromoteEmployee(newyears)
        Catch e As InvalidCastException
            Console.WriteLine("Error passing data to PromoteEmployee method. " + e.Message)
        End Try
    End Sub
End Class
using System;

public class Employee
{
    //Create employee level property.
    public int Emlevel
    {
        get
        {
            return(emlevel);
        }
        set
        {
            emlevel = value;
        }
    }

    private int emlevel = 0;
}

public class Ex13
{
    public static void PromoteEmployee(Object emp)
    {
        //Cast object to Employee.
        Employee e = (Employee) emp;
        // Increment employee level.
        e.Emlevel = e.Emlevel + 1;
    }

    public static void Main()
    {
        try
        {
            Object o = new Employee();
            DateTime newyears = new DateTime(2001, 1, 1);
            //Promote the new employee.
            PromoteEmployee(o);
            //Promote DateTime; results in InvalidCastException as newyears is not an employee instance.
            PromoteEmployee(newyears);
        }
        catch (InvalidCastException e)
        {
            Console.WriteLine("Error passing data to PromoteEmployee method. " + e.Message);
        }
    }
}
using namespace System;

public ref class Employee
{
public:
    Employee()
    {
        emlevel = 0;
    }

    //Create employee level property.
    property int Emlevel
    {
        int get()
        {
            return emlevel;
        }
        void set(int value)
        {
            emlevel = value;
        }
    }

private:
    int emlevel;
};

public ref class Ex13
{
public:
    static void PromoteEmployee(Object^ emp)
    {
        //Cast object to Employee.
        Employee^ e = (Employee^) emp;
        // Increment employee level.
        e->Emlevel++;
    }

    static void Main()
    {
        try
        {
            Object^ o = gcnew Employee();
            DateTime^ newyears = gcnew DateTime(2001, 1, 1);
            //Promote the new employee.
            PromoteEmployee(o);
            //Promote DateTime; results in InvalidCastException as newyears is not an employee instance.
            PromoteEmployee(newyears);
        }
        catch (InvalidCastException^ e)
        {
            Console::WriteLine("Error passing data to PromoteEmployee method. " + e->Message);
        }
    }
};

int main()
{
    Ex13::Main();
}

公共语言运行时捕捉 Catch 块没有捕捉的异常。 根据运行时的配置,或者出现一个调试对话框,或者程序停止执行并出现一个包含异常信息的对话框。 有关调试的信息,请参见调试和分析应用程序

请参见

任务

如何:使用 Try/Catch 块捕捉异常

如何:显式引发异常

如何:创建用户定义的异常

如何:使用 Finally 块

概念

异常类和属性

其他资源

异常处理基础知识