From 6a367eebb5689bf970b9d2a37ffb9a790a5b9e73 Mon Sep 17 00:00:00 2001 From: Yohan Choi Date: Thu, 31 Oct 2019 01:05:31 -0400 Subject: [PATCH] issue241-add content: try and catch --- JavaScript_Basics/try_and_catch.js | 71 +++++++++++++++++++++++++ docs/JavaScript_Basics/try_and_catch.md | 70 ++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 JavaScript_Basics/try_and_catch.js create mode 100644 docs/JavaScript_Basics/try_and_catch.md diff --git a/JavaScript_Basics/try_and_catch.js b/JavaScript_Basics/try_and_catch.js new file mode 100644 index 0000000..76b05b0 --- /dev/null +++ b/JavaScript_Basics/try_and_catch.js @@ -0,0 +1,71 @@ +//Try and Catch + +/* In JavaScript, try/catch/finally** statement handle errors that may occur in the block. + +try statement eables you to test your code in the block. +catch statement enables you to execute a block of code when it catches an error. +finally statement enables you to execute always after try and catch, regardless of an exception was thrown or caught. +*/ + + +// Syntax + +try { + throw 'exception'; // generate an exception +} catch (error) { + // statements to handle any exceptions +} finally { + // always runs regardless of the resulf ot try/catch +} + + +// Errors + +// Reference Error + +try { + hello(); // hello is not defined so it will cause a reference errorr +} catch (error) { + console.log(error); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} + + +// Type Error + +try { + null.hello(); // hello is not defined so it will cause a reference errorr +} catch (error) { + console.log("You cannot call from null"); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} + + +// Syntax Error + +try { + eval('2+2'); // thsi works fine + eval('Hello, World!'); // this will generate error +} catch (error) { + console.log("Syntax error"); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} + + +// User Defined Error + +const person = {name:John, age: 23}; + +try { + if(!person.gender){ + // throw 'person has no gender' + throw new SyntaxError('Person has no gender'); + } +} catch (error) { + console.log("You cannot call from null"); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} diff --git a/docs/JavaScript_Basics/try_and_catch.md b/docs/JavaScript_Basics/try_and_catch.md new file mode 100644 index 0000000..24ea13c --- /dev/null +++ b/docs/JavaScript_Basics/try_and_catch.md @@ -0,0 +1,70 @@ +# Try and Catch + +In JavaScript, **try**/**catch**/**finally** statement handle errors that may occur in the block. + +**try** statement eables you to test your code in the block. +**catch** statement enables you to execute a block of code when it catches an error. +**finally** statement enables you to execute always after try and catch, regardless of an exception was thrown or caught. + +## Syntax +```js +try { + throw 'exception'; // generate an exception +} catch (error) { + // statements to handle any exceptions +} finally { + // always runs regardless of the resulf ot try/catch +} +``` + +## Errors + +### Reference Error +```js +try { + hello(); // hello is not defined so it will cause a reference errorr +} catch (error) { + console.log(error); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} +``` + +### Type Error +```js +try { + null.hello(); // hello is not defined so it will cause a reference errorr +} catch (error) { + console.log("You cannot call from null"); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} +``` + +### Syntax Error +```js +try { + eval('2+2'); // thsi works fine + eval('Hello, World!'); // this will generate error +} catch (error) { + console.log("Syntax error"); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} +``` + +### User Defined Error +```js +const person = {name:John, age: 23}; + +try { + if(!person.gender){ + // throw 'person has no gender' + throw new SyntaxError('Person has no gender'); + } +} catch (error) { + console.log("You cannot call from null"); +} finally { + console.log('Finally runs reguardess of the reuslt'); +} +``` \ No newline at end of file