This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(x) { | |
if (typeof x !== 'number') { | |
throw new TypeError('x is not a number'); | |
} | |
return new Promise((resolve, reject) => { | |
resolve(x); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function run() { | |
try { | |
await foo(); | |
} catch(err) { | |
// This block will be reached now. | |
} finally { | |
// This block will be reached at the end. | |
} | |
} | |
run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
foo(); | |
} catch(err) { | |
// This block won’t be reached. | |
} finally { | |
// This block will be reached before the Promise is rejected. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
foo(); | |
} catch(err) { | |
// This block won’t be reached. | |
} finally { | |
// This block will be reached before the Promise is rejected. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return Promise.Reject(new Error()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function foo() { | |
throw new Error(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo2() { | |
try { | |
throw new Error(); | |
} catch { | |
return true; | |
} finally { | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo1() { | |
try { | |
return true; | |
} finally { | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
If (typeof x !== ‘number’) { | |
throw new TypeError(‘x is not a number’); | |
} else if (x <= 0) { | |
throw new RangeError(‘x should be greater than 0’); | |
} else { | |
// Do something useful | |
} | |
} catch (err) { | |
if (err instanceof TypeError) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
// a function that potentially throws an error | |
someFunction(); | |
} catch (err) { | |
// this code handles exceptions | |
console.log(e.message); | |
} finally { | |
// this code will always be executed | |
console.log(finally’); | |
} |