1try {
2 // Try to run this code
3}
4catch(err) {
5 // if any error, Code throws the error
6}
7finally {
8 // Always run this code regardless of error or not
9 //this block is optional
10}
1var someNumber = 1;
2try {
3 someNumber.replace("-",""); //You can't replace a int
4} catch(err) {
5 console.log(err);
6}
1try:
2 print("I will try to print this line of code")
3except:
4 print("I will print this line of code if an error is encountered")
5else:
6 print("I will print this line of code if there's no error encountered")
7finally:
8 print("I will print this line of code even if there's an error or no error encountered")
1async function promHandler<T>(
2 prom: Promise<T>
3): Promise<[T | null, any]> {
4 try {
5 return [await prom, null];
6 } catch (error) {
7 return [null, error];
8 }
9}
10
1try {
2 try_statements
3}
4catch (exception_var) {
5 catch_statements
6}
7finally {
8 finally_statements
9}
10