<p>I need to have my iPhone Objective-C code catch Javascript errors in a UIWebView.  That includes uncaught exceptions, syntax errors when loading files, undefined variable references, etc.</p>

<p>This is for a development environment, so it doesn't need to be SDK-kosher.  In fact, it only really needs to work on the simulator.</p>

<p>I've already found used some of the hidden WebKit tricks to e.g. expose Obj-C objects to JS and to intercept alert popups, but this one is still eluding me.</p>

<p>[NOTE: after posting this I did find one way using a debugging delegate.  Is there a way with lower overhead, using the error console / web inspector?]</p>

I have done this in firmware 1.x but not 2.x.
Here is the code I used in 1.x, it should at least help you on your way.
```
```
// Dismiss Javascript alerts and telephone confirms
/*- (void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
{
    if (button == 1)
    {
        [sheet setContext: nil];
    }

    [sheet dismiss];
}*/

// Javascript errors and logs
- (void) webView: (WebView*)webView addMessageToConsole: (NSDictionary*)dictionary
{
    NSLog(@"Javascript log: %@", dictionary);
}

// Javascript alerts
- (void) webView: (WebView*)webView runJavaScriptAlertPanelWithMessage: (NSString*) message initiatedByFrame: (WebFrame*) frame
{
    NSLog(@"Javascript Alert: %@", message);

    UIAlertSheet *alertSheet = [[UIAlertSheet alloc] init];
    [alertSheet setTitle: @"Javascript Alert"];
    [alertSheet addButtonWithTitle: @"OK"];
    [alertSheet setBodyText:message];
    [alertSheet setDelegate: self];
    [alertSheet setContext: self];
    [alertSheet popupAlertAnimated:YES];
}
```
```