What the... error handling! (try...catch) 
Tags: javascript, bugs, trycatch, errors
Published: 2018-05-08 13:53:38

---
title: What the... error handling! (try...catch) 
published: true
description: "Try...catch... finally... throw"  
tags: javascript,bugs,trycatch,errors 
---

#Please check your code!

**Bugs** exist... and they always will.

No matter how _good_ we are at code, or how _focused_ is our mind while coding... there will be **bugs** like _Coding bugs_ or _Input bugs_ or whatever.

##we have a duty ... to do our best to control them

A **very basic** way to control our code, is something really smart and easy.

Is there an error? Do this.
Not an error? Do this.
Anyway... do also this.

We can see this kind of stuff, in code like this:


```js
const breeds = ["labrador","chow-chow","samoyed"];

try
	{
      	//is there an
		akita;
      	//?
    }
catch(e)
	{
    	console.log("Ouch, no Akita here " + e);
        }
finally
	{
		console.log("At least there's a labrador");
	}

//-> Ouch, no Akita here ReferenceError: akita is not defined
//-> At least there's a labrador

```

So, what's happening here?

We created a simple:
`try{}
catch{}
finally{}`

###try{}

Here we're _trying_ to execute the code

```js
const breeds = ["labrador","chow-chow","samoyed"];

try
	{
      	//is there an
		akita;
      	//?
	}
```

As you can see there's an `akita` variable, but nothing in the program has to do with that, so there's an error

###catch{}

When an **error occurs** the `catch{}` statement is called.

```js
catch(e)
	{
    	console.log("Ouch, no Akita here " + e);
    }
```
All the _stuff inside_ the statement will be executed and we will _console out_ the String and that **strange** _e_.
That one is an _Error Object_ a built in _Object_ that _JS_ provides us to **capture** what's happening when an error occurs.

There are lot of stuff that can be done with It. If you use `e.stack` It will return _where in the stack_ the error occurred.

We can also define our _custom error_, later we'll see It.

###finally{}

This statement, always happens.
No matter if there's an error or the code is _good_ ... the `finally{}` will always be executed.

##our custom error...

Sometimes, It can be really important to create our _custom error_.

Why?

Maybe we want give a name to that _kind of error_.
Maybe we want a better code, more understandable.
There are lot of good reasons.

Here It's the `throw`

```js
const breeds = ["labrador","chow-chow","samoyed"];

  try
      {
          if(!breeds.includes("Akita")) 
          throw new SyntaxError("no Akita here");

      }
  catch(e)
      {
          console.log(e);
      }
//-> SyntaxError: no Akita here
```

See? We've just created a new "SyntaxError".
When the _try meets an error_ the _throw_ will immediately call the _catch_ and a _new Error_ will be created.

###We can go deeper with this.
What If we want to create a totally _new Error Object_ so that, the _Error_ will be thrown **only** if It's an instance of that _kind of Error_?

We can do this:


```js
class noDog extends Error {}

function findDog(arr) {
  if (arr.includes("Akita")) {
    return "Here you are";
  } else {
    throw new noDog("No Akita here");
  }
}

function doggy(arr) {
    try {
      return findDog(arr);
    } catch (e) {
      if (e instanceof noDog)
        throw e.message;
    }
}

console.log(doggy(["labradors","chow-chow"]));
//-> No Akita here
```

Hm... this is really not that easy like our previous examples.
So If you dunno what's happening here, don't be scared, It's ok.

We're defining a _class_ that _extends_ the built in _Error_.
We're **just** extending that class. Nothing more.
This new class has got no special properties, It's inheriting everything from the _Error_ Object.

In short... we've created a **new instance** for our _errors_.

With that in mind, we can now _catch_ exceptions **only** if those are **instanceof noDog**, and we can separate _this kind_ of error, from _standard one_.

<center>![wow](https://media.giphy.com/media/TzAorTGGXzkdO/giphy.gif)</center>
<center>COOL</center>