Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to use Try and Catch with Typescript

$
0
0

I want when the two arguments of function sum are numbers, the code success, but when one of the two arguments is not a number I want to throw an exception.

  const sum = (num1: number, num2: number) => {    return num1 + num2;  };  try {    typeof sum(8, 'A') === 'number';  } catch (e) {    console.log('the type you entered is NaN');  }

Now as a Test I put a string value instead num2 but the code ran without showing the exception in the consoleI mean it didn't log that 'the type you entered is NaN' from the catch block

catch (e) {    console.log('the type you entered is NaN');  }

I want to log that in console when arguments are not numbers, How to make that?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>