一旦抛出异常,try-catch 语句就会中断 try-block
try-catch statement interrupt try-block as soon as exception is thrown
研究:
根据MDN web docs:
The try...catch statement marks a block of statements to try, and
specifies a response, should an exception be thrown.
如果我理解正确,整个 try 块将被执行。这SOpost证实了我自己
我的问题:
有没有可能一抛出异常就中断一个try块进入catch块?
是:如何实现这种行为?
否:是否有其他方法可以实现此行为?
try {
console.log('omg');
throw new Error('omg');
console.log('ahhhhhh!!!!!');
} catch {
console.log('caught');
}
我认为 JS 可以满足您的需求。链接的 SO 用于 Java.
您链接的 SO post 是关于 Java,而不是 Java 脚本。
所以那里的答案可能不适用。
据我所知,catch 块将在抛出错误时被触发。
您可以随时抛出错误:
try {
console.log( 'before error: this should log' );
throw new Error( 'Trigger catch!' );
console.log( 'after error: this should not log' );
}
catch( error ) {
console.log( `catch block got the error: ${ error.message }` );
}
研究:
根据MDN web docs:
The try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.
如果我理解正确,整个 try 块将被执行。这SOpost证实了我自己
我的问题:
有没有可能一抛出异常就中断一个try块进入catch块?
是:如何实现这种行为?
否:是否有其他方法可以实现此行为?
try {
console.log('omg');
throw new Error('omg');
console.log('ahhhhhh!!!!!');
} catch {
console.log('caught');
}
我认为 JS 可以满足您的需求。链接的 SO 用于 Java.
您链接的 SO post 是关于 Java,而不是 Java 脚本。 所以那里的答案可能不适用。
据我所知,catch 块将在抛出错误时被触发。 您可以随时抛出错误:
try {
console.log( 'before error: this should log' );
throw new Error( 'Trigger catch!' );
console.log( 'after error: this should not log' );
}
catch( error ) {
console.log( `catch block got the error: ${ error.message }` );
}