有没有办法从异常处理程序中操作实例化的类属性?假设有一个具有名称和价格的类Automobile,并且在driver类中有一个自定义异常,我实例化该类并尝试创建汽车
String name = "car name";
Double price = 222.00;
try {
Automobile car = new Automobile(name, price) //creates the car object
}catch(CustomException e) {
e.fix()
}
在fix方法中,Custom异常实例化一个有效的car对象。如何将上面的car对象设置为异常生成的car对象。条件是Custom异常不能返回任何内容。
发布于 2020-05-09 08:07:25
您可以拆分声明和赋值,并在try
-block外部声明car
,如下所示:
String name = "car name";
Double price = 222.00;
Automobile car = null;
try {
car = new Automobile(name, price); //creates the car object
} catch (CustomException e) {
car = e.fix();
}
https://stackoverflow.com/questions/61693560
复制相似问题