相关疑难解决方法(0)

是否有可能捕获JavaScript异步回调中抛出的异常?

有没有办法捕获JavaScript回调中的异常?它甚至可能吗?

Uncaught Error: Invalid value for property <address>
Run Code Online (Sandbox Code Playgroud)

这是jsfiddle:http://jsfiddle.net/kjy112/yQhhy/

try {
    // this will cause an exception in google.maps.Geocoder().geocode() 
    // since it expects a string.
    var zipcode = 30045; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 5,
        center: new google.maps.LatLng(35.137879, -82.836914),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    // exception in callback:
    var geo = new google.maps.Geocoder().geocode({ 'address': zipcode }, 
       function(geoResult, geoStatus) {
          if (geoStatus != google.maps.GeocoderStatus.OK) console.log(geoStatus);
       }
    );
} catch (e) {
    if(e instanceof TypeError)
       alert('TypeError');
    else
       alert(e);
}?
Run Code Online (Sandbox Code Playgroud)

javascript google-maps exception callback

60
推荐指数
3
解决办法
3万
查看次数

标签 统计

callback ×1

exception ×1

google-maps ×1

javascript ×1