公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

我们可以使用 try-catch 语句来解决 TypeScript 中的错误。有时,我们需要在代码中添加多个try-catch块来处理多个错误。

当我们在代码中添加多个try-catch语句时,代码变得不可读,重构也成为开发者头疼的问题。在本教程中,我们将学习将过多的 try-catch 块转换为可以管理多个错误的单个 try-catch 块。

语法

用户可以按照以下语法在 TypeScript 中使用单个 try-catch 块。

try {
   throw new Error("error_message");
   // this code will not be executed
}
catch (error) {
   // manage the error
}

登录后复制

在上面的语法中,我们将错误抛出到 try 块中,并在 catch 块中捕获错误。

每当我们在 try 块中遇到任何错误时,执行控制都会直接转到 catch 语句,而不执行其他 try 块代码。

示例 1:try-catch 块过多

在下面的示例中,我们添加了四个 try-catch 块。我们从每个 try-catch 块中抛出带有不同消息的错误。

用户可以看到输出中每个 catch 块打印的错误消息。

try {
   console.log("Inside the first try block");
   throw new Error("first error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the second try block");
   throw new Error("second error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the third try block");
   throw new Error("Third error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the fourth try block");
   throw new Error("Fourth error message");
} catch (error) {
   console.log(error.message);
}

登录后复制

编译时,它将生成以下 JavaScript 代码。

try {
   console.log("Inside the first try block");
   throw new Error("first error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the second try block");
   throw new Error("second error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the third try block");
   throw new Error("Third error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the fourth try block");
   throw new Error("Fourth error message");
}
catch (error) {
   console.log(error.message);
}

登录后复制

输出

上述代码将产生以下输出 –

Inside the first try block
first error message
Inside the second try block
second error message
Inside the third try block
Third error message
Inside the fourth try block
Fourth error message

登录后复制

从上面的例子中,用户可以了解到当我们在单个代码中使用过多的 try-catch 语句时,代码是如何变得不可读且不清楚的。

现在,我们将学习使用单个 try-catch 块来处理具有不同错误的多个代码块。

示例 2

在下面的示例中,我们创建了solveProblems() 函数。它接受一个函数作为参数并在 try 块中调用该函数。如果函数抛出任何错误,我们可以在单个块中捕获它。

function func2() {
   throw new Error("This error is from second function!");
}

function func3() {
   let num = 10;
   num.toPrecision(1000);
}
function solveProblems(func: any) {
   // calling the callback function in the try block
   try {
      func();
   } catch (error) {
      console.log(error.message);
   }
}

// calling functions
solveProblems(func2);
solveProblems(func3);

登录后复制

编译时,它将生成以下 JavaScript 代码 –

function func2() {
   throw new Error("This error is from second function!");
}
function func3() {
   var num = 10;
   num.toPrecision(1000);
}
function solveProblems(func) {
   // calling the callback function in the try block
   try {
      func();
   }
   catch (error) {
      console.log(error.message);
   }
}
// calling functions
solveProblems(func2);
solveProblems(func3);

登录后复制

输出

上述代码将产生以下输出 –

This error is from second function!
toPrecision() argument must be between 1 and 100

登录后复制

从上面的示例中,用户可以了解如何通过将多个 try-catch 块替换为单个 try-catch 块来删除多个 try-catch 块。用户只需为每个单独的代码块创建一个单独的函数,并可以在单个 try 块中逐个调用每个函数。

以上就是如何解决Typescript中过多的try catch?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:Catch typescript 如何解决 过多
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定