Typescript 如何解决使用太多的try catch

Typescript 如何解决使用太多的try catch

在本文中,我们将试图了解如何在Typescript中编写太多的try/catch块来捕获多个错误,进一步地,我们将尝试通过某些编码示例来理解我们可以通过哪种技术来减少编写过多的try/catch块带来的开销。try-catch语句包含try块和catch块或finally块,或同时包含两个块。try块将首先执行,只有在抛出异常时catch块才会执行。最后的块在从整个代码控制流中退出之前总是执行。

try/catch块的必要性:

  • 简单来说,我们需要try/catch块来捕获从不同函数接收到的所有错误。
  • 捕获这些错误可以提高我们代码的性能,同时也提高了可读性。
  • 需要多个try/catch块来依次捕获多个函数的多个错误。

语法:

try {
    // Here we will all those variables or methods...
    // which tends to cause error or exception laterwards...
}

catch(error_variable){
    // do something with the error_variable...
    // either handle it or print it as per need...
}
...
// Many more such blocks could be added as per need....

让我们看一下下面的示例,它们将帮助我们了解有多少太多的try/catch块可以被创建,并且将看到更好的版本而不是太多的try/catch块。

示例-1: 在这个示例中,我们将创建多个try/catch块,以捕获来自不同函数的多个错误,这些函数实际上分别引发不同的错误,并且最后,我们将尝试依次打印这些错误消息。

Javascript

let first_function = (content: string): any => { 
    throw new Error(content); 
} 
  
let second_function = (content: string): any => { 
    throw new Error(content); 
} 
  
let third_function = (content: string): any => { 
    throw new Error(content); 
} 
  
let main_function = (): any => { 
    try { 
        let result = first_function( 
            "Error 404 !!...."); 
        console.log(result); 
    } 
    catch (error) { 
        console.log(error.message) 
    } 
  
    try { 
        let result = second_function( 
            "Something went wrong!!...."); 
        console.log(result); 
    } 
    catch (error) { 
        console.log(error.message) 
    } 
  
    try { 
        let result = third_function( 
            "Please try again later!!...."); 
        console.log(result); 
    } 
    catch (error) { 
        console.log(error.message) 
    } 
} 
main_function();

输出:

Error 404 !!....
Something went wrong!!....
Please try again later!!....

从上面的示例中可以看出,添加多个try/catch块实际上使代码显得更大,读起来也变得不太容易,没有任何用户会选择这样的方法。

现在,在另一个示例中,我们将尝试理解另一种在不使用多个try/catch块的情况下捕获多个函数抛出的错误的方法,比如寻找其他方法以减少多个try/catch块的开销。在这里,我们将使用回调函数的概念(即一个传递给另一个函数作为参数的函数,在第一个函数完成后执行,第一个函数本身就是主函数)。我们将使用一个回调函数来捕捉try块内每个方法抛出的错误,该回调函数将在一个包装函数(也称为辅助函数)中声明,通过该函数我们将按顺序捕捉所有函数的所有错误。

示例2: 在这个示例中,我们将使用带有try/catch块的回调函数来处理抛出的异常。

Javascript

let first_function = (content: string): any => { 
    throw new Error(content); 
}; 
  
let second_function = (content: string): any => { 
    throw new Error(content); 
}; 
  
let third_function = (content: string): any => { 
    throw new Error(content); 
}; 
  
let catchAllErrors = (callback: any, content: string): any => { 
    try { 
        callback(content); 
    } catch (error) { 
        return error; 
    } 
}; 
  
let main_function = () => { 
  
    let error_1 = catchAllErrors(first_function, 
        "Error 404!!..."); 
    let error_2 = catchAllErrors(second_function, 
        "Something went wrong!!..."); 
    let error_3 = catchAllErrors(third_function, 
        "Please try again later!!...."); 
  
    console.log("First Error: " + error_1); 
    console.log("Second Error: " + error_2); 
    console.log("Third Error: " + error_3); 
}; 
main_function();

输出:

First Error: Error: Error 404!!...
Second Error: Error: Something went wrong!!...
Third Error: Error: Please try again later!!....

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程