0

The Problem here is that with the output coming JavaScript also says undefined. I have seen some codes on w3schools.com and developers.mozilla websites and tried some other sites also but didn't find any solution or any explanation about the problem that i encounter.

I want to throw an exception when 0 or negative value occur.

function isPositive(a) {

  try {
    if (a > 0) return "YES";

    if (a < 0) throw "Negative Value";

    if (a == 0) throw "Zero Value";
  } catch (error) {
    console.log(error);
  }
}


console.log(isPositive(0));

Output expected was "Zero Value"

Actual Output is

"Zero Value" 

undefined 

Any suggestions why my code prints undefined along with the output? Thanks in Advance.

4
  • 1
    Where are you running this? Also, will you please post the rest of your code. Commented Mar 26, 2019 at 6:30
  • You don't return anything unless the result is positive. And why are you throwing in a try? Commented Mar 26, 2019 at 6:32
  • this is all code that i have. i am testing my code on jsbin.com. I have already tried without try and catch but it was not working. Commented Mar 26, 2019 at 6:46
  • You have two console.log in that code. What do you expect the second one to log? Commented Mar 26, 2019 at 7:02

3 Answers 3

1

This is because if any error is thrown in try it doesnot stop execution of code. If you want to stop the function on error just remove that try-catch and simply return

function isPositive(a){
    if(a>0) return "YES"
    if(a<0) return "Negative Value";
    if(a == 0) return "Zero Value";
}
console.log(isPositive(0));

If you want to throw error put the the statements out of try

function isPositive(a){
    if(a>0) return "YES"
    if(a<0) throw ("Negative Value");
    if(a == 0) throw ("Zero Value");
}
console.log(isPositive(0));

Sign up to request clarification or add additional context in comments.

2 Comments

I have also tried to put the if statements out of the try and catch block but it still doesn't work and giving "Uncaught Zero Value (line 12)".
@Amit Khatri So whats problem it that. If you want "Zero Value" only then use the first snippet
0

function isPositive(a){
try{
if(a>0) return "YES";

   if(a<0) throw "Negative Value";

   if(a == 0) throw "Zero Value";
}catch(err)
{
return err;
}
}


console.log(isPositive(0));

I find the answer and this code works for me and giving the proper results.

Comments

0

You have console.log(isPositive(a)) calling the function and if it throws error it returns "undefined" (and you will have it printed into console). But if you specify that you return the error it will print the "Negative Error". Code bellow.

function isPositive(a) {
    try{
        if (a > 0) return "YES" 
        if (a  == 0) throw ('Zero Error');    
        if (a < 0) throw ('Negative Error');
    }
    catch(error){
        return(error);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.