最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can you bubble up errors so they can be caught in the same trycatch block? - Stack Overflow

programmeradmin2浏览0评论

I've got an object with functions that throw errors,

myObj = {
  ini:function(){
    this.f();
  },
  f:function(){
   throw new Error();
  } 
};

but I only want to catch the exceptions where the object is created

try{
  var o = new myObj();
}catch(err){
  alert("error!");
}

it looks like i have to have try/catch blocks everywhere =/ to capture the error event in different function scopes

try{
    myObj = {
      ini:function(){
        try{
          this.f();
        }catch(err){
         alert("f threw an err");
        }
      },
      f:function(){
       throw new Error();
      } 
    };
}catch(err){
 alert("error happend while crating Obj");
}

But I only want to capture from one place =/ How do I do this?

I've got an object with functions that throw errors,

myObj = {
  ini:function(){
    this.f();
  },
  f:function(){
   throw new Error();
  } 
};

but I only want to catch the exceptions where the object is created

try{
  var o = new myObj();
}catch(err){
  alert("error!");
}

it looks like i have to have try/catch blocks everywhere =/ to capture the error event in different function scopes

try{
    myObj = {
      ini:function(){
        try{
          this.f();
        }catch(err){
         alert("f threw an err");
        }
      },
      f:function(){
       throw new Error();
      } 
    };
}catch(err){
 alert("error happend while crating Obj");
}

But I only want to capture from one place =/ How do I do this?

Share Improve this question edited Mar 9, 2010 at 20:28 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked Mar 9, 2010 at 20:01 qodeninjaqodeninja 11.3k32 gold badges102 silver badges154 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

Have your function throw a specific type of object, and then in your catch block check to see if (err instanceof MyExceptionObj) and handle appropriately, otherwise re-throw it.

By re-throw I mean:

If the caught exception object isn't one you can handle, you should re-throw it to give any catch blocks further up a chance to handle it. If none do, the browser will catch it and display a JS error.

try {
   if ($.browser.msie) {
      throw new UnsupportedBrowserException();
   }
} catch (ex) {
   if (ex instanceof UnsupportedBrowserException) {
      alert('Your browser isn't supported.');
   } else {
      // We don't know how to handle this exception, throw it back.
      throw ex;
   }
}

You probably wouldn't do this in the real world.

You can only conditionally catch exceptions in JavaScript 1.7 and higher. Refer to the following:

try {
  0();
} catch (ex if ex instanceof TypeError) {
  // only catch TypeErrors
}

Otherwise, you have to catch all exceptions, do an instanceof check, and then rethrow the exceptions if the check returns true.

maybe instead of throwing an error and then catching it, you could just call a function.

发布评论

评论列表(0)

  1. 暂无评论