MCQs on Error Handling | TypeScript

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

  1. 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
  2. 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 {}
  3. 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
  4. 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");
  5. 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
  6. In TypeScript, which block is used to execute code regardless of whether an error occurred?
    a) catch
    b) finally
    c) try
    d) catch-finally
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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 {}
  3. 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
  4. When defining a custom error class, which constructor parameter is commonly included?
    a) message
    b) errorCode
    c) timestamp
    d) errorType
  5. 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
  6. 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
  7. 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");
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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; }
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

QnoAnswer
1b) TypeScript has the same error handling as JavaScript but with more strict checks
2a) try {} catch {} finally {}
3a) Using throw new Error()
4a) throw new Error("Something went wrong");
5c) It contains code that might throw an error
6b) finally
7b) Runtime errors that occur during code execution
8a) When handling unexpected runtime exceptions
9b) TypeScript will stop execution immediately
10a) throw new MyError("Custom message");
11a) By extending the built-in Error class
12a) class MyError extends Error {}
13a) Custom properties like status codes or error messages
14a) message
15a) By adding properties to the constructor
16c) By extending a built-in error class and adding properties
17a) throw new MyError("Custom message");
18a) They allow better debugging and error tracking
19a) Yes, you can create and throw different custom error types
20a) By adding them in the constructor and assigning them to this
21c) A value that never occurs or is unreachable
22b) A function that always throws an exception or loops indefinitely
23a) function throwError(message: string): never { throw new Error(message); }
24a) It throws a compile-time error
25a) They must always throw an error or enter an infinite loop
26b) When a function’s return type is always unreachable
27b) Throwing an exception
28a) It allows TypeScript to enforce that a function cannot return a value
29b) No, never cannot be assigned to any other type
30b) A function that returns a value

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top