-3

when i google what does throw do, the answer i get is that it throws an exception. wow. that means nothing to me at all.

I have since learned thanks to stackoverflow that it tosses the error until the closest handler does something with it.

There is a question on this already but it doesn't answer my question at all. what does throw look like if you don't handle it? Does it alert the user? Does it show up only in console? does it hit someone in my neighborhood and cause them to knock on my door at 3am?

can someone PLEASE break down what this looks like so I can visualize it in terms of real world analogy. Like an exception is like putting trash into a bag and putting in on your sidewalk for pickup if it doesn't get picked up.... I don't know. Please help with that explanation.

  • "it tosses the error until the closest handler does something with it" What else do you want to know? It's just a message. You can do something with it or you can ignore it. – jmargolisvt Aug 13 '20 at 21:20
  • What do you mean what else do I want to know? I want to know if it's visible. What it looks like. What you just said is like saying to a blind person, "lights light up the path." – shayne rush Aug 14 '20 at 22:23

2 Answers2

1

I personally like the MDN summary of throw:

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

The linked documentation also has examples that might help you understand throw better.

To answer the question:

what does throw look like if you don't handle it? Does it alert the user? Does it show up only in console?

The answer is "yes", it does show up in your console. Often in bright red with an error stack, which shows you where it was thrown.

console.log("Hello");
throw "example error"; // also visible in JavaScript console of your browser
console.log("World!");
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
0

The answer is simply "it depends". The implementation defines what happens to exceptions which aren't caught by user code. All browsers I know of will just log it in the console and stop executing that script or function, and otherwise let the page work as normal. Node.js will print the error to STDERR and terminate the process with a non-zero exit code.

In both cases the exception is still "handled" by the process executing the user code.

Klaycon
  • 10,599
  • 18
  • 35