Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

try catch error typescript

// doc: https://devblogs.microsoft.com/typescript/announcing-typescript-4-0/#unknown-on-catch
try {  /* ... */ }
catch (e: unknown) { // <-- note `e` has explicit `unknown` type
    e.message // errors
    if (typeof e === "string") {
        e.toUpperCase() // works, `e` narrowed to string
    } else if (e instanceof Error) {
        e.message // works, `e` narrowed to Error
    }
    // ... handle other error types 
}


// Typescript 4.4 added the ability to make unknown the default on catch variables using the useUnknownInCatchVariables  flag.
Comment

typescript catch error type

class MyError extends Error {
  constructor(message: string) {
    super(message);
    Object.setPrototypeOf(this, MyError.prototype)
  }
}

const myFunction = () => {
  throw new MyError('some reason');
}

try {
  myFunction();
} catch (error) {
  if (error instanceof MyError) {
    // Handle MyError....
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: sum of digits in c++ 
Typescript :: nextjs global prisma 
Typescript :: Redirects in Odoo Website 
Typescript :: Bulk Products Selection on sales process odoo 
Typescript :: ts class without implementation 
Typescript :: react typescript tailwind toggle button 
Typescript :: sass migrate division vue 
Typescript :: adonis auth register 
Typescript :: how to print list as matrix in python without brackets 
Typescript :: A Tree Diagram is a drawing with branches of all possible outcomes 
Typescript :: cmd check if folder exists or not 
Typescript :: nodemon.ps1 cannot be loaded because running scripts is disabled on this system. 
Typescript :: scroll to top angular 
Typescript :: install ng bootstrap 
Typescript :: ion select active by button 
Typescript :: Error: Running Homebrew as root is extremely dangerous and no longer supported. As Homebrew does not drop privileges on installation you would be giving all build scripts full access to your system. 
Typescript :: ValueError: Cannot run multiple SparkContexts at once; 
Typescript :: value of input in typescript 
Typescript :: style mat-dialog-container 
Typescript :: python loop two 
Typescript :: angular change how date looks 
Typescript :: react native social share 
Typescript :: online meeting platforms 
Typescript :: open rails secrets file 
Typescript :: find a value in list of objects in c# 
Typescript :: eslint absolute imports error 
Typescript :: sort two lists that refence each other 
Typescript :: typescript type object 
Typescript :: typescript function example array arg 
Typescript :: typescript react elements 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =