避免用try catch
There are always been times when we make errors (which is a good thing sometimes) which cause to freeze the program, the program will run to the line where the error exists then it will freeze (crash) the program surely no one wants it. So there are plenty of ways and one of them is "try catch".
有时候,我们犯错误(有时是一件好事)会导致程序冻结,该程序将运行到存在错误的行,然后冻结(崩溃)程序,肯定没有人想要它。 因此,有很多方法,其中之一是“尝试捕获” 。
Example:
例:
const convertToRs = (dollar) => dollar * 65
let myValue = convertToRs(5)
console.log(myValue);
Output
输出量
325
The above code we used arrow functions (Read: ARROW FUNCTIONS) have a potential error, now let's take a case where a user gets input '$5' instead of '5', then what will happen?
上面我们使用的箭头函数(阅读: ARROW FUNCTIONS )有一个潜在的错误,现在让我们来看一个用户输入为'$ 5'而不是'5'的情况 ,那会发生什么?
const convertToRs = (dollar) => dollar * 65
let myValue = convertToRs('$5')
console.log(myValue)
Output
输出量
NaN
Obviously, we don’t want it so we need to try something.
显然,我们不想要它,所以我们需要尝试一些东西。
One thing we can do is check the type of the value of dollar using if...else to be a number instead of something else. Let's see an example:
我们可以做的一件事是使用if ... else将其作为数字而不是其他值来检查美元价值的类型。 让我们来看一个例子:
const convertToRs = (dollar) =>{
if (typeof dollar === 'number')
return dollar*65
else{
throw Error('Type of input is wrong')
}
}
let myValue = convertToRs('$5')
console.log(myValue);
console.log('This is the end');
Output
输出量
Explanation:
说明:
So in the above example, we used if...else to compare the typeof dollar to number, if it is true then it will calculate the required answer but if false then it will throw an error. Throw keyword used to throw the user-defined errors, we can simply throw ‘type of input is wrong’ but it can be done using a console log too so what the point of using throw the error so we used Error() an interface error. In the output, we can see the first line it is the path of the program and at last it the line mentioned where the error was thrown. This is a great way to get to understand an error but without a catch block, our program will freeze at this point. Notice the last console log never get as an output. So we will use try-catch in the next section:
所以在上面的例子中,我们使用,如果...别的typeof运算美元比较数字,如果是真的,那么它会计算所需要的答案,但如果为假,则它会抛出一个错误 。 Throw关键字用于引发用户定义的错误,我们可以简单地引发“输入类型错误”,但是也可以使用控制台日志来完成,因此使用该方法的目的是引发错误,因此我们使用Error()接口错误。 在输出中,我们可以看到第一行是程序的路径,最后是提到的引发错误的行。 这是了解错误的好方法,但是如果没有catch块,我们的程序将在此时冻结。 注意最后一个控制台日志永远不会作为输出。 因此,我们将在下一部分中使用try-catch :
Example:
例:
const convertToRs = (dollar) =>{
if (typeof dollar === 'number')
return dollar*65
else{
throw Error('Type of input is wrong')
}
}
try{
letmyValue = convertToRs('$5')
console.log(myValue);
}
catch (error) {
console.log(error)
}
console.log('This is the end');
Output
输出量
Now in the above example, we put that part of the code in the try block from which we expect some kind of error or exceptions. Try block throws any kind of exception that occurs and catch block actually catches that error and prevents the program from freezing like in the above example we get the same kind of error message but still, we get our last line as output mean our program run completely without freezing up and having errors.
现在,在上面的示例中,我们将代码的那一部分放在try块中 ,从中我们期望出现某种错误或异常。 try块会引发发生的任何类型的异常,并且catch块实际上会捕获该错误并防止程序冻结,就像在上面的示例中一样,我们得到了相同类型的错误消息,但是仍然得到了最后一行,因为输出表示我们的程序完全运行没有冻结和有错误。
翻译自: https://www.includehelp.com/code-snippets/try-catch-to-avoid-program-freeze-in-javascript.aspx
避免用try catch