[go: up one dir, main page]

0% found this document useful (0 votes)
6 views8 pages

CS31-AppDev - Module 5

The document provides an overview of error and exception handling in Dart programming, focusing on the use of try-catch blocks, custom exceptions, and finally clauses for cleanup. It differentiates between exceptions and errors, explaining how to throw, catch, and assert exceptions in Dart. Additionally, it outlines the structure and flow of exception handling in Dart, emphasizing the importance of managing runtime errors gracefully.

Uploaded by

Xander Pardico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

CS31-AppDev - Module 5

The document provides an overview of error and exception handling in Dart programming, focusing on the use of try-catch blocks, custom exceptions, and finally clauses for cleanup. It differentiates between exceptions and errors, explaining how to throw, catch, and assert exceptions in Dart. Additionally, it outlines the structure and flow of exception handling in Dart, emphasizing the importance of managing runtime errors gracefully.

Uploaded by

Xander Pardico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

CS31-AppDev (Android Development and Emerging Technologies)

College of Computer Studies

I. INTRODUCTION
Error and exception handling in dart allows you to
gracefully manage and respond to runtime errors
that may occur during the execution of your
program. Dart provides many ways to handle
exceptions and error handling using try-catch
Module 5 blocks, custom exception classes, and finally block
for cleanup operations.
Dart Exceptions
II. OBJECTIVES
and Error Handling 1. Define what exceptions and errors
are in the context of Dart
programming.
2. Differentiate between exceptions
and errors, and understand when
and why they occur.
3. Learn how to use try-catch blocks to
handle exceptions and understand
the structure and flow of try-catch-
finally blocks.

III. PRELIM INARY ACTIVITIES


N/A

Page 1 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

IV. LESSON PROPER


A. Dart Exceptions
You dart code can throw and catch exceptions. Exceptions are errors indicating that something
unexpected happened.

In contrast to Java, all of Dart's exceptions are unchecked exceptions. Methods don't declare which
exceptions they might throw, and you aren't required to catch any exceptions.

Page 2 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

B. Throw
Here's an example of throwing, or raising, an exception:

Note:

Because throwing an exception is an expression, you can throw exceptions in => statements, as well as
anywhere else that allows expressions:

Page 3 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

C. Catch
Catching, or capturing, an exception stops the exception from propagating (unless you rethrow the
exception). Catching an exception gives you a chance to handle it:

To handle code that can throw more than one type of exception, you can specify multiple catch
clauses. The first catch clause that matches the thrown object's type handles the exception. If the catch
clause does not specify a type, that clause can handle any type of thrown object:

Page 4 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

As the preceding code shows, you can use either on or catch or both. Use on when you need to specify
the exception type. Use catch when your exception handler needs the exception object.

You can specify one or two parameters to catch(). The first is the exception that was thrown, and the
second is the stack trace (a StackTrace object).

To partially handle an exception, while allowing it to propagate, use the rethrow keyword.

Page 5 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

D. Finally
To ensure that some code runs whether or not an exception is thrown, use a finally clause. If
no catch clause matches the exception, the exception is propagated after the finally clause runs:

The finally clause runs after any matching catch clauses:

Page 6 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

E. Assert
During development, use an assert statement— assert(<condition>, <optionalMessage>); —to disrupt
normal execution if a boolean condition is false.

To attach a message to an assertion, add a string as the second argument to assert (optionally with
a trailing comma):

The first argument to assert can be any expression that resolves to a boolean value. If the expression's
value is true, the assertion succeeds and execution continues. If it's false, the assertion fails and an
exception (an AssertionError) is thrown.

Page 7 of 8
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

V. PRACTICE EXERCISES/ACTIVITIES
N/A

VI. ADDITIONAL RESOURCES


N/A

VII. ASSESSMENT

VIII. REFERENCES
https://dart.dev/language/error-handling

Page 8 of 8

You might also like