MCQs on Error Handling Basics | Dart

Mastering error handling in Dart is crucial for writing robust and maintainable code. This chapter covers understanding exceptions, using try-catch-finally, and the throw keyword for effective error management.


1. Understanding Exceptions

  1. What is an exception in Dart?
    a) A syntax error in the code
    b) A runtime error or unexpected condition
    c) A warning during compilation
    d) An optimization method
  2. Which class serves as the base class for all exceptions in Dart?
    a) Error
    b) Exception
    c) Throwable
    d) RuntimeError
  3. What is the purpose of exceptions in Dart?
    a) To optimize performance
    b) To manage errors and unusual conditions
    c) To ensure security
    d) To improve code readability
  4. How does Dart handle uncaught exceptions by default?
    a) Ignores them
    b) Stops program execution and logs the error
    c) Automatically retries the operation
    d) Silently fixes the error
  5. Which of the following is NOT a built-in exception in Dart?
    a) FormatException
    b) ArgumentError
    c) FileNotFoundException
    d) IntegerDivisionByZeroException
  6. What does Exception represent in Dart?
    a) A critical error
    b) A recoverable error condition
    c) A warning
    d) A deprecated feature
  7. Can you define custom exceptions in Dart?
    a) Yes, by extending the Exception class
    b) No, only built-in exceptions are allowed
    c) Yes, but only as runtime errors
    d) No, Dart does not support custom exceptions
  8. Which function is used to handle unexpected errors globally in Dart?
    a) runApp()
    b) FlutterError.onError
    c) runZonedGuarded()
    d) handleException()
  9. What type of exceptions does Dart encourage catching?
    a) All runtime errors
    b) Specific and recoverable exceptions
    c) Compilation errors
    d) Warning messages
  10. What is the difference between Error and Exception in Dart?
    a) Error is for syntax issues, Exception for runtime errors
    b) Error represents unrecoverable issues, Exception represents recoverable errors
    c) They are identical in functionality
    d) Error is used only in debug mode

2. try-catch-finally

  1. What is the purpose of the try block in Dart?
    a) To define code that might throw exceptions
    b) To handle exceptions directly
    c) To optimize code performance
    d) To execute cleanup tasks
  2. Which block executes when an exception occurs in a try block?
    a) try
    b) catch
    c) finally
    d) throw
  3. Can a catch block handle multiple types of exceptions in Dart?
    a) No, it handles only one type at a time
    b) Yes, but only for user-defined exceptions
    c) Yes, using a generic catch block
    d) No, Dart does not support catch
  4. What is the purpose of the finally block?
    a) To rethrow the exception
    b) To ensure cleanup code runs regardless of exceptions
    c) To terminate program execution
    d) To log errors
  5. In Dart, is the finally block optional?
    a) Yes, it is optional
    b) No, it is mandatory
    c) Yes, but only for user-defined exceptions
    d) No, only in async operations
  6. What happens if an exception is thrown in the catch block?
    a) It is ignored
    b) The finally block still executes
    c) The program terminates immediately
    d) The exception is logged silently
  7. How can you access the exception object in the catch block?
    a) By specifying a variable after catch
    b) Using throw
    c) By defining it in the finally block
    d) By using a static method
  8. Which of the following syntax is correct for a try-catch-finally block in Dart?
    a) try {} catch (e) {} finally {}
    b) try (e) {} catch {} finally {}
    c) try {} finally {} catch (e) {}
    d) try {} {catch (e)} finally {}
  9. How can you catch a specific exception type in Dart?
    a) Using catch (e as ExceptionType)
    b) Using on ExceptionType catch (e)
    c) Using try-catch-finally
    d) Using throw ExceptionType
  10. What happens when neither catch nor finally blocks are used with try?
    a) Code will not compile
    b) The exception propagates up the call stack
    c) The program automatically resolves the exception
    d) The try block runs normally

3. throw Keyword

  1. What does the throw keyword do in Dart?
    a) Defines a new exception
    b) Logs an exception
    c) Creates and raises an exception
    d) Executes cleanup tasks
  2. Which of the following is a correct way to throw an exception in Dart?
    a) throw new Exception('Error occurred')
    b) raise Exception('Error occurred')
    c) emit Exception('Error occurred')
    d) catch Exception('Error occurred')
  3. Can the throw keyword be used to raise custom exceptions?
    a) Yes, by defining and throwing a custom exception class
    b) No, only built-in exceptions are allowed
    c) Yes, but only with async code
    d) No, custom exceptions are not supported in Dart
  4. What is the syntax to rethrow an exception in Dart?
    a) rethrow;
    b) throw;
    c) raise;
    d) emit;
  5. What happens when you use rethrow in a catch block?
    a) It propagates the exception to the next level
    b) It creates a new exception instance
    c) It suppresses the exception
    d) It stops program execution
  6. Can throw be used outside of a try block?
    a) Yes, exceptions can be thrown anywhere
    b) No, it must always be inside try
    c) Yes, but only for asynchronous code
    d) No, Dart does not support it
  7. How can you provide additional details with a thrown exception?
    a) Pass a message or value to the exception
    b) Use the catch block to customize
    c) Attach metadata to the exception
    d) All of the above
  8. What does the following code do?dartCopy codethrow FormatException('Invalid format'); a) Logs the error
    b) Throws a FormatException with a message
    c) Suppresses the error
    d) Executes cleanup tasks
  9. When using throw, what type of object can be thrown?
    a) Only Exception objects
    b) Any object
    c) Only strings
    d) Only Error objects
  10. Is the throw keyword mandatory for raising exceptions in Dart?
    a) Yes
    b) No, Dart has alternative methods
    c) Yes, but only for runtime exceptions
    d) No, exceptions cannot be raised manually

Answers Table

QNoAnswer (Option with Text)
1b) A runtime error or unexpected condition
2b) Exception
3b) To manage errors and unusual conditions
4b) Stops program execution and logs the error
5c) FileNotFoundException
6b) A recoverable error condition
7a) Yes, by extending the Exception class
8c) runZonedGuarded()
9b) Specific and recoverable exceptions
10b) Error represents unrecoverable issues, Exception represents recoverable errors
11a) To define code that might throw exceptions
12b) catch
13c) Yes, using a generic catch block
14b) To ensure cleanup code runs regardless of exceptions
15a) Yes, it is optional
16b) The finally block still executes
17a) By specifying a variable after catch
18a) try {} catch (e) {} finally {}
19b) Using on ExceptionType catch (e)
20b) The exception propagates up the call

Here are the next 10 answers for the MCQs:

QNoAnswer (Option with Text)
31a) To define code that might throw exceptions
32b) catch
33c) Yes, using a generic catch block
34b) To ensure cleanup code runs regardless of exceptions
35a) Yes, it is optional
36b) The finally block still executes
37a) By specifying a variable after catch
38a) try {} catch (e) {} finally {}
39b) Using on ExceptionType catch (e)
40b) The exception propagates up the call stack

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