0

In a UIWebView's delegate method webView:shouldStartLoadWithRequest:navigationType:, I put an NSAssert there, but it just output a log, instead of terminating. Here is my code:

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
            navigationType:(UIWebViewNavigationType)navigationType
{
    NSAssert(NO,@"assertion in delegate");
    return YES;
}

and the output:

*** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: assertion in delegate

rmaddy
  • 314,917
  • 42
  • 532
  • 579
zhao yang
  • 33
  • 2
  • 1
    see this once it helps you http://stackoverflow.com/questions/7883899/ios-5-uiwebview-delegate-webkit-discarded-an-uncaught-exception-in-the-webview – Anbu.Karthik Jun 13 '16 at 03:54

2 Answers2

0

Failed NSAsserts raise ObjC exceptions. (NSInternalInconcistencyException to be precise.) Anyone can install exception handlers or other mechanisms to define what happens to exceptions that are raised in code that they call. And those mechanisms don't have to include halting the process (though continuing after an exception is generally not a great idea).

When you raise ObjC exceptions in a callback, you're not guaranteed that execution will terminate as a result — you're at the mercy of whatever exception handling was set up by the code that called you. If you want to bring the whole process crashing down due to some failure in delegate code, it's probably best to abort() it yourself.

rickster
  • 124,678
  • 26
  • 272
  • 326
0

NSAssert raises an Objective-C exception and these can be caught, so it doesn't guarantee your program will be aborted. Using it in your own code is generally fine, but if your code is called by a framework - such as when a delegate is invoked - it depends on what the framework does. As you have discover WebKit catches exceptions and discards or handles them itself.

The simple solution is to use the standard assert() function. This takes a single Boolean expression and will abort the program printing out the expression, file name and line number of the assertion. This function does not use Objective-C exceptions, it uses the standard abort() function, and so cannot be caught.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86