Why, when I am testing that a method throws an exception and the method throw an exception, does the test stop?

前端 未结 1 1072
清歌不尽
清歌不尽 2021-01-21 11:16

I have a unit test that tests if method throws an exception when condition is present, and method does throws exception as expected.

相关标签:
1条回答
  • 2021-01-21 11:47

    Why does the test stop when the execution is thrown?

    Because you have a breakpoint, which stops execution.

    Why, after removing the breakpoint, does my application crash when the exception is thrown?

    Because you have an unhandled exception. Unhandled exceptions cause your program to crash.

    How can I handle an exception so it won't crash my program?

    The easy answer to this question is to simply NOT throw an exception. In other programming languages, like Java, this is perfectly standard. But in Objective-C, we don't really do exceptions. In Objective-C, exceptions should be saved for TRULY exceptional behavior.

    With that said, and a strong suggestion for you to find another way to handle whatever it is you're trying to handle, this is how you handle an exception in Objective-C:

    @try {
        // code that could throw an exception
    }
    
    @catch (NSException *e) {
        // handle the exception...
    }
    
    @finally {
        // post try-catch code, executed every time
    }
    
    0 讨论(0)
提交回复
热议问题