Aracılığıyla paylaş


Catch bloğunda belirli özel durumları kullanma

Genel olarak, temel catch bir deyim kullanmak yerine belirli bir istisna türünü yakalamak iyi bir programlama uygulamasıdır.

Bir istisna oluştuğunda, yığın boyunca iletilir ve her catch bloğuna bunu ele alma fırsatı verilir. Catch deyimlerinin sırası önemlidir. Özel durumlara özgü catch bloklarını, genel bir özel durum yakalama bloğundan önce yerleştirin, aksi takdirde derleyici hata mesajı oluşturabilir. Uygun catch bloğu, özel durumun türü catch bloğunda belirtilen özel durumun adıyla eşleştirilerek belirlenir. Belirli bir catch bloğu yoksa, özel durum, varsa, genel bir catch bloğu tarafından yakalanır.

Aşağıdaki kod örneği, bir try/catch bloğu ile bir InvalidCastException yakalamak için kullanılır. Örnek, tek bir özellik, çalışan düzeyi (Employee ) ile adlı Emlevel bir sınıf oluşturur. Bir yöntem, PromoteEmployee bir nesne alır ve çalışan seviyesini artırır. Bir InvalidCastException, bir DateTime örneği PromoteEmployee yöntemine geçirildiğinde meydana gelir.

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.
        var e = (Employee) emp;
        // Increment employee level.
        e.Emlevel = e.Emlevel + 1;
    }

    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);
        }
    }
}
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

Ayrıca bakınız