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 :: how to check if a value exists in unorderedmaps 
Typescript :: scripted testing and exploratory testing 
Typescript :: enums in typescript 
Typescript :: alphabets range using re 
Typescript :: pandas get count of pair of elements in two columns 
Typescript :: ts new example 
Typescript :: How to pass optional parameters while omitting some other optional parameters? 
Typescript :: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break Identify output ? 
Typescript :: typescript react function coponent props 
Typescript :: typescript run on save 
Typescript :: java stack remove elements which equals the top element 
Typescript :: rest parameters in typescript 
Typescript :: using typescript with vue 
Typescript :: The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.40 and higher. flutter compressvideo 
Typescript :: how to pass data to another page in ionic 3 
Typescript :: typescript interface 
Typescript :: Electron WebContents context-menu 
Typescript :: json in typescript 
Typescript :: how to define array of object type in typescript 
Typescript :: typescript omit 
Typescript :: typescript to c# converter 
Typescript :: typescript globalThis 
Typescript :: sort even dont exists meta wordpress 
Typescript :: how to show account related contacts on click of a button using lightnig components 
Typescript :: how to call an action from another action slice in redux 
Typescript :: types of project plan 
Typescript :: dynamic key 
Typescript :: display only user contributor comments wordpress 
Typescript :: ____________ determines the time spent in various parts of the unit. 
Typescript :: how to pass data between requests in api 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =