Handling exceptions with try..catch..finally in TypeScript
This article will discuss try..catch..finally
handling exceptions in TypeScript using the statement.
Handling exceptions in TypeScript
In TypeScript, try..catch..finally
a block handles exceptions that occur during program runtime. It allows the program to run correctly and not end randomly.
The main code where an exception might occur is placed try
inside the block. If an exception occurs, it goes to the block that handles it catch
; however, if no error is encountered, catch
the block is skipped.
In any case, finally
the block will always execute regardless of whether errors occur in the program.
try..catch..finally
Below are some code examples that illustrate how we can use exception handling
in TypeScript .
function doOrThrow<T>(error: T): true{
if (Math.random() > .5){
console.log('true')
return true;
}
else{
throw error;
}
}
try{
doOrThrow('err1');
doOrThrow('err2');
doOrThrow('err3');
}
catch (e:any){
console.log(e,'error')
}
finally{
console.log("Terminated");
}
Output:
try
The function inside the block is called three times, and it is passed one argument. If the number generated is greater than 0.5, it executes if
the block and returns true
.
Otherwise, it will throw an error which catch
is handled in the block. It tells which call it will throw after executing try
and .catch
Let’s consider another example.
let fun: number; // Notice use of `let` and explicit type annotation
const runTask=()=>Math.random();
try{
fun = runTask();
console.log('Try Block Executed');
throw new Error("Done");
}
catch(e){
console.log("Error",e);
}
finally{
console.log("The Code is finished executing.");
}
Output:
unknown
Type catch
clause variables
Prior to TypeScript's 4.0 版
, catch
clause variables were typed any
. Since variables had the type assigned to them any
, they lacked type safety, leading to invalid operations and errors.
Now, TypeScript's __INITIALIZE__ 4.0 版
allows us to specify catch
the types of clause variables unknown
, which is any
much safer than __INITIALIZE__ .
It reminds us that we need to perform some kind of checking before operating on a value.
try {
// ...
}
catch (err: unknown) {
// error!
// property 'toUpperCase' does not exist on 'unknown' type.
console.log(err.toUpperCase());
if (typeof err === "string") {
// works!
// We have narrowed 'err' down to the type 'string'.
console.log(err.toUpperCase());
}
}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
在 TypeScript 中使用 try..catch..finally 处理异常
Publish Date:2023/03/19 Views:396 Category:TypeScript
-
本文详细介绍了如何在 TypeScript 中使用 try..catch..finally 进行异常处理,并附有示例。
在 TypeScript 中使用 declare 关键字
Publish Date:2023/03/19 Views:257 Category:TypeScript
-
本教程指南通过特定的实现和编码示例深入了解了 TypeScript 中 declare 关键字的用途。
在 TypeScript 中 get 和 set
Publish Date:2023/03/19 Views:971 Category:TypeScript
-
本篇文章演示了类的 get 和 set 属性以及如何在 TypeScript 中实现它。
在 TypeScript 中格式化日期和时间
Publish Date:2023/03/19 Views:276 Category:TypeScript
-
本教程介绍内置对象 Date() 并讨论在 Typescript 中获取、设置和格式化日期和时间的各种方法。
在 TypeScript 中返回一个 Promise
Publish Date:2023/03/19 Views:590 Category:TypeScript
-
本教程讨论如何在 TypeScript 中返回正确的 Promise。这将提供 TypeScript 中 Returns Promise 的完整编码示例,并完整演示每个步骤。
在 TypeScript 中定义函数回调的类型
Publish Date:2023/03/19 Views:1454 Category:TypeScript
-
本教程说明了在 TypeScript 中为函数回调定义类型的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。
在 TypeScript 中把 JSON 对象转换为一个类
Publish Date:2023/03/19 Views:530 Category:TypeScript
-
本教程演示了如何将 JSON 对象转换为 TypeScript 中的类。
使用 NPM 将 TypeScript 更新到最新版本
Publish Date:2023/03/19 Views:455 Category:TypeScript
-
本教程说明了如何使用 npm 更新到最新版本的 TypeScript。这将为如何使用 npm 将 TypeScript 更新到最新版本提供完整的实际示例。
使用 jQuery 和 TypeScript
Publish Date:2023/03/19 Views:251 Category:TypeScript
-
本教程提供了使用 jQuery 和 TypeScript 的基本理解和概念。