Doubt about exceptions and Try/catch blocks

Asked

Viewed 45 times

1

I would like to know what is the "correct way" to write a Try/catch block:

async function ping() {
  try {
    undefined();
    return 'pong';
  } catch (error) {
    console.log('fn ping:', error);
  }
}

async function chamar() {
  try {
    const resultado = await ping();
    console.log(resultado);
  } catch (error) {
    console.log('fn chamar:', error);
  }
}

chamar();

Output when calling a function not defined within the "ping" function with Try/catch:

fn ping: TypeError: undefined is not a function

However, when Try/catch is not used in the ping function, the result is as follows::

fn chamar: TypeError: undefined is not a function

Which would be the correct way since the ping function will always be called inside a Try/catch. Also, I want to treat all errors in the "call" function. Have a problem not using Try/catch in functions that will already be called within another Try/catch?

  • Where is this function pong? Also, is there a reason you are annotating these functions with async?

  • The correct was function ping, sorry for the error. I sent only one example, but in the project I will use the asynchronous functions

1 answer

1


There is no right or wrong in this case. It depends on your need. In relation to language, both are correct.

The difference in output is due to the fact that when some exception is released in Javascript, the block try most "next" catching the exception. Thus, how the error occurred within the function ping, if you have, just above the code that throws error, a try/catch, this will capture the error, since it is the closest.

If you remove this try/catch, he seeks another one above, which in the example of the question is the try/catch of function chamar.


In this example, one can say that the try/catch of ping is unnecessary, since you are doing the Runtime launch TypeError of purpose (which in theory should be captured by tools in the development environment). However, to capture errors of this kind, which can eventually go unnoticed, it is always ideal to have a try/catch "", next to the entry-point of your code, which will handle untreated exceptions.

In this case, it can be said that the entry-point of your code is the function chamar, and the try/catch inside her is playing the role of global error Handler.

Generally, it is used try/catch in "deeper" codes (as in ping) when you call some function or perform some operation that can throw some exception. For example, while reading a file, run a query in database, etc. Thus, it is ideal that you have a very specific exception treatment for the operation you are doing.

It is interesting to highlight the contrast between the specific error treatment (treated in the previous paragraph) and the overall error treatment, which is much more generic.

Read more about blocks try/catch in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.