如何使try块中的所有代码在catch语句执行之前运行
本文关键字:语句 catch 执行 运行 代码 try 何使 | 更新日期: 2023-09-27 18:27:26
我必须尝试实例化三个对象,其中两个将抛出异常,但我希望在抛出异常之前运行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;
}
}
Is there anyway to do this without using three different try blocks?
没有。
catch
的全部目的是处理发生的任何异常。
如果你只想写更少的代码,你可以把它封装在一个函数中
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;
}
}
}
您可以这样做:
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
}
现在你有一个尝试捕获,但你重复使用它:)
请注意,这是在一个文本板中写的,语法来自内存。如果它抱怨,你可能需要调整Tuple也许。。。但它应该足够近。
您可以从不同的角度看待这个问题,并将它们分离到另一个方法中。失败的员工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;
}
}