Dart Exception Handling
Exception Handling in Dart (OOP)
In Dart, exception handling is used to manage errors and other unexpected situations that can occur during the execution of a program. Exception handling allows developers to write code that can gracefully handle errors rather than causing the entire program to crash.
Dart provides the try
, catch
, and finally
blocks for handling exceptions:
- try: Wraps the code that might throw an exception.
- catch: Catches the exception and allows you to handle it.
- on: Specifies the type of exception you want to catch.
- finally: Executes code after
try
andcatch
blocks, regardless of whether an exception was thrown.
Example of Exception Handling in Dart (OOP)
Suppose we are building a BankAccount
class where each account has an accountHolder
and a balance
. We will implement a withdraw
method that throws an exception if an attempt is made to withdraw an amount greater than the balance.
Code Example:
Explanation:
Custom Exception (
InsufficientFundsException
):- A custom exception class is created by implementing the
Exception
interface. - It includes a method
errorMessage()
that returns an error message.
- A custom exception class is created by implementing the
BankAccount Class:
BankAccount
has two properties,accountHolder
andbalance
.- The
withdraw
method checks if the withdrawal amount is greater than the balance.- If it is, it throws an
InsufficientFundsException
. - If not, it subtracts the amount from
balance
and prints the updated balance.
- If it is, it throws an
Exception Handling in
main()
:- First
try-catch
Block:- The program attempts to withdraw $300, which is within the balance, so it is successful.
- Second
try-on-catch-finally
Block:- The program attempts to withdraw $800, which exceeds the balance, causing an
InsufficientFundsException
. - The
on InsufficientFundsException
block catches the exception, and thecatch
block prints a custom error message. - The
finally
block executes regardless of whether the exception was thrown, indicating that the transaction is completed.
- The program attempts to withdraw $800, which exceeds the balance, causing an
- First
Output:
Benefits of Exception Handling in Dart OOP
- Increased Reliability: Proper exception handling ensures the program can handle unexpected errors gracefully, preventing crashes.
- Improved Code Clarity: By handling exceptions in specific
try
,catch
,on
, andfinally
blocks, the code is more structured and readable. - Custom Error Messages: Custom exception classes allow for more informative error messages, improving the debugging process.
- Better User Experience: Instead of terminating the program abruptly, exceptions can be handled to provide meaningful feedback, making the user experience smoother.
Summary
Exception handling in Dart, especially with custom exception classes in OOP, provides a robust mechanism to handle errors without disrupting the flow of a program. In the BankAccount
example, we demonstrated how to throw and catch a custom exception for insufficient funds, enhancing error feedback and ensuring that the program continues executing smoothly.