如何在try语句执行之前运行try块中的所有代码

时间:2014-10-22 21:08:34

标签: c#

我必须尝试实例化三个对象,其中两个将抛出异常,但我想在抛出异常之前使try块中的所有代码都运行。无论如何不使用三个不同的试块来做到这一点吗?

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Employee EmPy1 = new Employee("111-11-111", -4.0);
            Employee EmPy2 = new Employee("222-22-222", 7.5);
            Employee EmPy3 = new Employee("333-33-333", 750);
        }
        catch (EmployeeException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

class Employee
{
    private string _id;
    private double _hourlyWage;
    public double HourlyWage
    {
        get { return _hourlyWage; }
        set
        {
            if (value < 0 || value > 255)
            {
             throw new EmployeeException("Value must be greater than 0 and less than 254");   
            }
            else
            {
                _hourlyWage = value;
            }
        }
    }
    public Employee(string Id, double hourlyWage)
    {
        _id = Id;
        HourlyWage = hourlyWage;
    }
}

4 个答案:

答案 0 :(得分:7)

Is there anyway to do this without using three different try blocks?

没有。

catch的重点是处理发生的任何异常。

答案 1 :(得分:2)

如果您只想编写更少的代码,可以将其封装在函数

class Program
{
    static void Main(string[] args)
    {
        Employee EmPy1 = TryCreateCmployee("111-11-111", -4.0);
        Employee EmPy2 = TryCreateCmployee("222-22-222", 7.5);
        Employee EmPy3 = TryCreateCmployee("333-33-333", 750);
    }

    static Employee TryCreateCmployee(string id, double hourlyWage)
    {
        try
        {
            return new Employee(id, hourlyWage);
        }
        catch (EmployeeException ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }

    }
}

答案 2 :(得分:0)

您可以这样做:

class Program
{
    static void Main(string[] args)
    {
        Wrapper(/* have your ids-salarys here as a tupel */)
    }
}

public Tuple(List<Employee>,List<Exception>) Builder(string anId, double aSalary){

    Employee EmPy = new Employee(anId,aSalary);
}

public Tuple(List<Employee>,List<Exception>) Wrapper(IEnumerable<Tuple<string,double> aTuple){
    var employees = new List<Employee>();
    var exceptions = new List<Excetpion>();

    foreach (var t in aTuple){
       try
        {
             Builder(t.First,t.Second);
             employees.Add(EmPy);
        }
        catch (EmployeeException ex)
        {
            exceptions.Add(ex)
        } 
    }
}

class Employee
{
    // snip snip
}

现在你有一次尝试捕获,但你重复使用它:)

请注意,这是在文本板中编写的,语法来自内存。如果它抱怨,你可能需要调整元组可能......但它应该足够接近。

答案 3 :(得分:0)

您可以稍微改变一下这个问题并将它们与另一种方法分开。失败的员工ID可以存储在列表中,与成功创建的员工一样。然后您可以稍后处理它们。

这是你可以做到的一种方式

class MyProgram
{
    private static List<string> _failedEmployeeIds;

    private static List<Employee> _successfullyCreatedEmployees;

    static void Main(string[] args)
    {
        _failedEmployeeIds = new List<string>();
        _successfullyCreatedEmployees = new List<Employee>();

        TryCreateEmployee("111-11-111", -4.0);
        TryCreateEmployee("222-22-222", 7.5);
        TryCreateEmployee("333-33-333", 750);

        ProcessFailedEmployees(); // do something here
        ProcessCreatedEmployees(); // do something here
    }

    static void TryCreateEmployee(string employeeId, double employeeWage)
    {            
        try
        {
            var employee = new Employee(employeeId, employeeWage);
            _successfullyCreatedEmployees.Add(employee);
        }
        catch (EmployeeException ex)
        {
            Console.WriteLine(ex.Message);
            _failedEmployeeIds.Add(employeeId);
        }
    }
}

class Employee
{
    private string _id;
    private double _hourlyWage;
    public double HourlyWage
    {
        get { return _hourlyWage; }
        set
        {
            if (value < 0 || value > 255)
            {
                throw new EmployeeException("Value must be greater than 0 and less than 254");
            }

            _hourlyWage = value;
        }
    }
    public Employee(string Id, double hourlyWage)
    {
        _id = Id;
        HourlyWage = hourlyWage;
    }
}