Skip to content

Fixed Issue241: add try and catch #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions JavaScript_Basics/try_and_catch.js
Original file line number Diff line number Diff line change
@@ -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');
}
70 changes: 70 additions & 0 deletions docs/JavaScript_Basics/try_and_catch.md
Original file line number Diff line number Diff line change
@@ -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');
}
```