throw

throw – Throw an exception.

Description

throw expression

Parameters

Name Description Type Default Optional
expression No

Examples

Example #1 – throw example
try { throw 42; console.log('Never executed.'); } catch (e) { console.log(e); // 42 }
Example #2 – throw example
try { throw new Error(42); console.log('Never executed.'); } catch (e) { console.log(e); // Error: 42(…) console.log(e.message); // 42 console.log(e.name); // Error }
Example #3 – throw example
function myException (code, message) { this.name = 'myException'; this.code = code; this.message = message; } try { throw new myException(42, 'Custom exception.'); console.log('Never executed.'); } catch (e) { console.log(e.code); // 42 console.log(e.message); // Custom exception. console.log(e.name); // myException }

External references