Chapter 13 of TypeScript focuses on effective error handling techniques, including using custom error types and the never type to manage exceptional scenarios. Mastering these concepts ensures cleaner and safer code.
Multiple Choice Questions (MCQs)
Section 1: Error Handling in TypeScript
How does TypeScript handle errors compared to JavaScript? a) TypeScript doesn’t have any error handling mechanism b) TypeScript has the same error handling as JavaScript but with more strict checks c) TypeScript throws compile-time errors only d) TypeScript uses try and catch exclusively
Which of the following is a valid error handling structure in TypeScript? a) try {} catch {} finally {} b) try {} catch {} c) catch {} finally {} d) try {} exception {}
How can you explicitly handle errors in TypeScript? a) Using throw new Error() b) By using catch block only c) Through try block only d) TypeScript doesn’t allow explicit error handling
Which of the following is a valid syntax for throwing an error in TypeScript? a) throw new Error("Something went wrong"); b) throw new Exception("Error"); c) throw "Error"; d) throw Error("Error occurred");
What is the role of the try block in error handling? a) It catches and processes errors b) It throws errors explicitly c) It contains code that might throw an error d) It executes code after an error is thrown
In TypeScript, which block is used to execute code regardless of whether an error occurred? a) catch b) finally c) try d) catch-finally
What type of errors can TypeScript catch during runtime? a) Only syntax errors b) Runtime errors that occur during code execution c) Type-related compile-time errors d) Errors caused by invalid type assignments only
In which situation would you typically use a try-catch block? a) When handling unexpected runtime exceptions b) To enforce strict types in variables c) To assign default values to variables d) To initialize variables with a specific type
How does TypeScript handle uncaught exceptions? a) TypeScript will automatically fix the exception b) TypeScript will stop execution immediately c) TypeScript doesn’t handle uncaught exceptions d) TypeScript will ignore uncaught exceptions and continue
How can you throw a custom error in TypeScript? a) throw new CustomError(); b) throw CustomError("message"); c) throw Error(); d) TypeScript doesn’t allow custom errors
Section 2: Custom Error Types
How can you define a custom error type in TypeScript? a) By extending the built-in Error class b) By creating a function that returns an error c) By using throw new ErrorType(); d) Custom error types are not allowed in TypeScript
What is the correct way to create a class that extends Error in TypeScript? a) class MyError extends Error {} b) class MyError extends TypeError {} c) class MyError implements Error {} d) class MyError creates Error {}
What additional feature can a custom error type have in TypeScript? a) Custom properties like status codes or error messages b) It can only have an error message c) It can’t have additional properties d) Custom errors do not work in TypeScript
When defining a custom error class, which constructor parameter is commonly included? a) message b) errorCode c) timestamp d) errorType
How can you include a custom property in a custom error type? a) By adding properties to the constructor b) By directly modifying the Error class c) By assigning custom properties in the catch block d) TypeScript doesn’t allow custom properties in errors
Which method can be used to provide additional context to a custom error class? a) By adding a message property only b) By overriding the toString() method c) By extending a built-in error class and adding properties d) TypeScript doesn’t allow additional context in custom errors
How would you instantiate and throw a custom error with a message in TypeScript? a) throw new MyError("Custom message"); b) throw new Error("Custom message"); c) throw new CustomError(); d) throw MyError("Custom message");
What is the benefit of using custom error types in TypeScript? a) They allow better debugging and error tracking b) They reduce the number of errors thrown c) They make error handling slower d) TypeScript doesn’t benefit from custom error types
Can you throw multiple types of custom errors in TypeScript? a) Yes, you can create and throw different custom error types b) No, only one custom error type is allowed c) Custom error types are not allowed d) TypeScript automatically handles multiple errors
How would you add additional error properties in a custom error class? a) By adding them in the constructor and assigning them to this b) By modifying the error message directly c) By calling super() with an extra argument d) You cannot add properties to custom errors
Section 3: Using the Never Type
What does the never type represent in TypeScript? a) A type that allows any value b) A function that returns null c) A value that never occurs or is unreachable d) A type that always throws an error
Which of the following is a valid use case of the never type in TypeScript? a) A function that doesn’t return any value b) A function that always throws an exception or loops indefinitely c) A function that returns undefined d) A function with no return type
Which of the following is an example of a function that returns never? a) function throwError(message: string): never { throw new Error(message); } b) function noReturn(): void { return; } c) function invalid(): number { return 42; } d) function alwaysReturns(): any { return 1; }
What happens if you try to assign a never value to a variable? a) It throws a compile-time error b) It automatically converts to null c) It is allowed only with strict types d) It assigns the never value successfully
Which statement is true regarding functions with never as the return type? a) They must always throw an error or enter an infinite loop b) They can return any value c) They always return undefined d) They can return any primitive data type
How does TypeScript infer the never type in certain cases? a) When there is no return statement b) When a function’s return type is always unreachable c) When an object has no properties d) When there is no exception thrown
In TypeScript, which of the following would cause a function to be typed as never? a) Returning undefined b) Throwing an exception c) Returning null d) Returning a boolean value
What is a key benefit of using the never type? a) It allows TypeScript to enforce that a function cannot return a value b) It helps avoid errors related to unreachable code c) It automatically handles all errors in the code d) It makes error handling easier
Can never be assigned to any other type? a) Yes, never can be assigned to any type b) No, never cannot be assigned to any other type c) never can only be assigned to null d) never can only be assigned to undefined
Which of the following is NOT an appropriate usage of the never type in TypeScript? a) A function that always throws an error b) A function that returns a value c) A function that enters an infinite loop d) A function that never completes execution
Answer Key
Qno
Answer
1
b) TypeScript has the same error handling as JavaScript but with more strict checks
2
a) try {} catch {} finally {}
3
a) Using throw new Error()
4
a) throw new Error("Something went wrong");
5
c) It contains code that might throw an error
6
b) finally
7
b) Runtime errors that occur during code execution
8
a) When handling unexpected runtime exceptions
9
b) TypeScript will stop execution immediately
10
a) throw new MyError("Custom message");
11
a) By extending the built-in Error class
12
a) class MyError extends Error {}
13
a) Custom properties like status codes or error messages
14
a) message
15
a) By adding properties to the constructor
16
c) By extending a built-in error class and adding properties
17
a) throw new MyError("Custom message");
18
a) They allow better debugging and error tracking
19
a) Yes, you can create and throw different custom error types
20
a) By adding them in the constructor and assigning them to this
21
c) A value that never occurs or is unreachable
22
b) A function that always throws an exception or loops indefinitely
23
a) function throwError(message: string): never { throw new Error(message); }
24
a) It throws a compile-time error
25
a) They must always throw an error or enter an infinite loop
26
b) When a function’s return type is always unreachable
27
b) Throwing an exception
28
a) It allows TypeScript to enforce that a function cannot return a value