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
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
Which class serves as the base class for all exceptions in Dart? a) Error b) Exception c) Throwable d) RuntimeError
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
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
Which of the following is NOT a built-in exception in Dart? a) FormatException b) ArgumentError c) FileNotFoundException d) IntegerDivisionByZeroException
What does Exception represent in Dart? a) A critical error b) A recoverable error condition c) A warning d) A deprecated feature
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
Which function is used to handle unexpected errors globally in Dart? a) runApp() b) FlutterError.onError c) runZonedGuarded() d) handleException()
What type of exceptions does Dart encourage catching? a) All runtime errors b) Specific and recoverable exceptions c) Compilation errors d) Warning messages
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
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
Which block executes when an exception occurs in a try block? a) try b) catch c) finally d) throw
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
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
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
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
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
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 {}
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
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
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
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')
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
What is the syntax to rethrow an exception in Dart? a) rethrow; b) throw; c) raise; d) emit;
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
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
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
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
When using throw, what type of object can be thrown? a) Only Exception objects b) Any object c) Only strings d) Only Error objects
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
QNo
Answer (Option with Text)
1
b) A runtime error or unexpected condition
2
b) Exception
3
b) To manage errors and unusual conditions
4
b) Stops program execution and logs the error
5
c) FileNotFoundException
6
b) A recoverable error condition
7
a) Yes, by extending the Exception class
8
c) runZonedGuarded()
9
b) Specific and recoverable exceptions
10
b) Error represents unrecoverable issues, Exception represents recoverable errors
11
a) To define code that might throw exceptions
12
b) catch
13
c) Yes, using a generic catch block
14
b) To ensure cleanup code runs regardless of exceptions
15
a) Yes, it is optional
16
b) The finally block still executes
17
a) By specifying a variable after catch
18
a) try {} catch (e) {} finally {}
19
b) Using on ExceptionType catch (e)
20
b) The exception propagates up the call
Here are the next 10 answers for the MCQs:
QNo
Answer (Option with Text)
31
a) To define code that might throw exceptions
32
b) catch
33
c) Yes, using a generic catch block
34
b) To ensure cleanup code runs regardless of exceptions