<script language="Javascript"> try { throw new Error(10,"asdasdasd") } catch (e) { alert(e.message); alert(e.description) alert(e.number) alert(e.name) throw new Error(10,"asdasdasd") } </script>
try { foo.bar(); } catch (e) { alert(e.name + ": " + e.message); }
EvalError: raised when an error occurs executing code in eval() RangeError: raised when a numeric variable or parameter is outside of its valid range ReferenceError: raised when de-referencing an invalid reference SyntaxError: raised when a syntax error occurs while parsing code in eval() TypeError: raised when a variable or parameter is not a valid type URIError: raised when encodeURI() or decodeURI() are passed invalid parameters
new Error(); new Error("异常信息");
try { throw new Error("Whoops!"); } catch (e) { alert(e.name + ": " + e.message); }
try { foo.bar(); } catch (e) { if (e instanceof EvalError) { alert(e.name + ":" + e.message); } else if (e instanceof RangeError) { alert(e.name + ": " + e.message); } // etc }
try { foo.bar(); } catch (e) { if (browserType != BROWSER_IE) { alert("name: " + e.name + "message: " + e.message + "lineNumber: " + e.lineNumber + "fileName: " + e.fileName + "stack: " + e.stack); } else { alert("name: " + e.name + "errorNumber: " + (e.number & 0xFFFF ) + "message: " + e.message"); } }
try { throw new Date(); // 抛出当前时间对象 } catch (e) { alert(e.toLocaleString()); // 使用本地格式显示当前时间 }
위 내용은 자바스크립트에서 catch 사용법을 소개합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!