DEV Community

Cover image for Try {Catch/Except} All The Way
Hik Hik
Hik Hik

Posted on

Try {Catch/Except} All The Way

Javascript & Python

In Python, we don't call it try-catch but rather try-except which works similarly to the try-catch in javascript and it is dedicated to error-handling.

PYTHON STRUCTURE
The program tries to run the code inside the try block, if run fails, the exemption is raised in the except block.

For example:

try:
  print(x)
except:
  print("Exemption: x was not defined. ")
Enter fullscreen mode Exit fullscreen mode

The above python code will return an exemption:

Exemption: x was not defined.
Enter fullscreen mode Exit fullscreen mode

JAVASCRIPT STRUCTURE
It is simple in javascript since error object array has message which summaries the whole error.

For example we want to print x in the console.

try {
     console.log(x)
} catch (error) {
     console.log(error.message);
}
Enter fullscreen mode Exit fullscreen mode

The above javascript code will print:

x is not defined
Enter fullscreen mode Exit fullscreen mode

Top comments (0)