neerajDorle
neerajDorle

Reputation: 540

understanding exception handling in JavaScript: get different output when changed the place of try/catch block

I am new to learning JavaScript and sort of stuck up while learning exception handling. I have understood the thing that whenever an exception occurs , it is thrown using "throw" keyword and similarly it is caught using "catch" block.

But what I am unable to understand is that , I have a small , simple code that demonstrates simple Exception handling technique, and in that code whenever I change the place of the catch block, I get different outputs. Here is the simple code and its different o/p s depending upon where I place the catch block.

function lastElement(array) {
     if (array.length > 0)
        return array[array.length - 1];
     else
        throw "Can not take the last element of an empty array.";
}

function lastElementPlusTen(array) {
     return lastElement(array) + 10;
}

try {
   print(lastElementPlusTen([])); 
}
catch (error) {
    print("Something went wrong: ", error);
}

the o/p that I get here is as expected :

Something went wrong: Can not take the last element of an empty array.

now when I add the try/catch block around the function lastElementPlusTen: like this

function lastElement(array) {
   if (array.length > 0)
     return array[array.length - 1];
   else
     throw "Can not take the last element of an empty array.";
}



 try  {

   function lastElementPlusTen(array) {
   return lastElement(array) + 10;
   }

 }
catch (error) {
    print("Something went wrong: ", error);
}


print(lastElementPlusTen([]));

now the o/p that I get here is :

Exception: "Can not take the last element of an empty array."

the "something went wrong" from the catch block is not printed.

why is this so??similarly when I place the try/catch block around different pieces of code (for ex: around the first function , body of the lastElementPlusTen function etc) I get different o/p s. why is this happening. How does exception handling work??

Upvotes: 0

Views: 126

Answers (2)

PSL
PSL

Reputation: 123739

You are not catching the exception in your second case. It just throws the exception unhandled not printing it as you expect, place

print(lastElementPlusTen([]));

inside try..catch

Try:

function lastElement(array) {
    if (array.length > 0) return array[array.length - 1];
    else throw "Can not take the last element of an empty array.";
}

function lastElementPlusTen(array) {
    return lastElement(array) + 10;
}

try { //<-- this is where you need try.. catch not at the function definision
   print(lastElementPlusTen([])); //<-- this invokes the error.

} catch (error) {
    print("Something went wrong: ", error);
}

Demo Watch the console for the log

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102723

The problem is that you're putting the try/catch around the function declaration -- the error is not thrown there, it's thrown when the function is actually invoked. So you need this instead:

// this code block will not throw any error, although it will when the function is invoked
function lastElementPlusTen(array) {
   return lastElement(array) + 10;
}

try{
    console.log(lastElementPlusTen([]));
}
catch (error) {
    console.log("Something went wrong: ", error);
}

Fiddle demo

Upvotes: 1

Related Questions