r/learnjavascript • • Dec 19 '21

Help with Error Handling and Try/Catch

How do you fire a specific function based on the error received from a try block?

I am working through the javascript track on exercism and am having trouble with the third part of the Factory Sensors problem. Any help would be greatly appreciated.

5 Upvotes

3 comments sorted by

View all comments

Show parent comment

4

u/slvfox Dec 19 '21

Thank you so much!

export function monitorTheMachine(actions) {
  try {
    actions.check()
  } catch(error){
    if (error instanceof ArgumentError){
      actions.alertDeadSensor()
    }

The issue is I don't understand why the if in the catch block isn't working. Am I somehow calling the instance of the error wrong?

2

u/Notimecelduv Dec 19 '21

That might depend on your previous code but if the try-block doesn't throw an error, then the function will return whatever the try-block returns and the catch-block will not be used.