在setter中引发异常

时间:2016-09-29 13:23:16

标签: c# .net oop

我有一个有财产的班级。在这个属性中,我试图验证数据表不是null。如果为null则会生成异常。

我的问题是如果我想在标签控件中显示错误消息,如何在调用类中捕获它。

这是属性:

- (void)testExample2 {
    XCUIApplication *app = [[XCUIApplication alloc] init];
    XCUIElement *cfWindow = app.windows[@"Celsius Fahrenheit Converter"];

    XCUIElement *textField1 = [[cfWindow childrenMatchingType:XCUIElementTypeTextField]elementBoundByIndex:0];

    XCUIElement *textField2 = app.textFields[@"fahrField"];

    [textField1 typeText:@"21\r"];

    NSString *str = (NSString *) textField2.value;

    XCTAssertEqual(str, @"69.8");
}

我在asp.net应用程序中使用它。

2 个答案:

答案 0 :(得分:4)

在调用类中使用try-catch块,并在catch块中显示消息:

try
{
    // use DtNotes here, throws an ex.
}
catch (ArgumentOutOfRangeException ex)
{
    // display message here.
}

答案 1 :(得分:0)

var myClass = new MyClass();
try { 
   myClass.DtNotes = null; //Null assignment throws exception
} catch (ArgumentOutOfRangeException ex){ //Catch the exception
   string message = ex.Message; //Retrieve the message
   //You can display the message here
}