Results 1 to 4 of 4

Thread: I cannot understand 'throw' statement!

  1. #1
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I cannot understand 'throw' statement!

    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:

    Code:
    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:

    Code:
    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?



    Thanks!

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    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:
    Code:
    ]
    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
    Code:
    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.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!


Similar Threads

  1. Change syntax from Update Case Statement to Update Union Statement
    By newphpcoder in forum MySQL and other databases
    Replies: 0
    Last Post: 11-11-2011, 01:23 AM
  2. Help me to understand these codes!?
    By newbe123 in forum JavaScript
    Replies: 3
    Last Post: 12-09-2010, 12:52 PM
  3. i dont understand what this is... please help
    By rdavies2502 in forum HTML
    Replies: 0
    Last Post: 09-25-2009, 12:20 PM
  4. JS does'nt throw error on undeclared var.
    By rangana in forum JavaScript
    Replies: 3
    Last Post: 05-13-2008, 01:11 PM
  5. Help me understand this code
    By giban in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 12-29-2007, 03:31 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •