PDA

View Full Version : I cannot understand 'throw' statement!



yankleber
09-11-2014, 03:32 PM
Hi!

It will seem a silly question for some but I absolutelly cannot understand the 'throw' statement in try..catch.

Normally I catch exceptions by doing something like this:


try
{
// try something
}
catch (e);
{
CustomError(e.message);
}

However I have told recently that 'throw' the error is a better approach. After to see some (useless for me) online tutorials the only thing I got was some examples where the 'throw' is put where I normally would put my own calls like this:


try
{
// try something
}
catch (e)
{
throw(e);
}

I tried it but nothing (apparently) happened. No alert, no output in Firebug, no nothing. For where the 'thrown' error is sent or where it is outputed??? And what is the actual advantage on use it instead to just treat the error myself?

:confused:

Thanks!

Deadweight
09-12-2014, 02:32 AM
Okay i hope i can clear this up.
First of all you are using throw wrong...

as you know a try catch (finally) looks like this:

]
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
finally {
Block of code to be executed regardless of the try / catch result
}


and here is an example of using throw inside a try/catch


function myFunction()
var message, x;
message = document.getElementById("message");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "Empty";
if(isNaN(x)) throw "Not a number";
if(x > 10) throw "Too high";
if(x < 5) throw "Too low";
}
catch(err) {
message.innerHTML = "Error: " + err + ".";
}
finally {
document.getElementById("demo").value = "";
}
}


As you can see it throws the message to the catch block. Then you have when you want your error to say. So what goes wrong in the try block gets thrown into the catch block.
Think of your catch block as its own function and the throw is how you call the function.

jscheuer1
09-12-2014, 04:38 PM
The directive to throw is independent. You can use it anytime anywhere. It will throw a string or number to the error handler (console in today's browsers). It's great though because it will not generate an additional error if there is no console or other error handler. You don't need to use it at all. However, it's built into javascript, so is better for that purpose than anything else you can use that you create/write yourself. It's a natural in try catch and in try catch finally constructs if you want custom error handling. But, as I say you can use it anywhere in code for other error handling, ex:


if(!document.getElementById){throw 'Get a real browser!';}

I don't know where this would be applicable today, but you get the idea - doesn't require try, catch, etc.

Deadweight's examples look good as well. I just wanted to add about throw being independent, and how well suited for custom errors it is. It can also be used to throw the official error (possibly only under certain circumstances - depends how you write it) after it has been held back by catch.

yankleber
09-12-2014, 05:18 PM
Oh, I think I got it. I thought that it would do something by itself, but seems that the 'throw' is just a way of create custom messages, is that?

Gosh, someone has told me that I had to use that so I thought that I was missing something magically neat!

Thanks for explain it to me!

:)