Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

Exceptions and Exception Handling in Dart Programming

exception in dart


Exceptions and Exception Handling

An exception is an abnormal condition that disrupts the normal flow of execution in any program. It may occur during the execution time (also called runtime) of the said program. When an execution exception is raised, it stops the other parts of the program from working as well. Dart has an implementation to handle such conditions, which makes use of exception handlers. Execution handlers are statements written to handle exceptions/errors and prevent the program from stopping abruptly.


Exception handling in Dart

Every execution is a sub-type of the Exception class. These executions that occur in Dart must be handled with execution handlers, thus preventing the program from stopping or terminating abruptly.

a. Try and Catch Block:

The try block encloses code that can cause execution. The on block is utilized to mention the specific execution that is to be handled. The catch block is utilized when a handler requires a generic execution object.

void main() {
  int a = 5;
  int b = 0;
  int res;
  try {
    res = a ~/ b;
  } on IntegerDivisionByZeroException {
    print('Cannot divide by zero');
  }
}



b. finally Block

The finally block includes the code that should be executed irrespective of an exception occurring. It is given after the try/on/catch block.

void main(){
 int a = 5;
 int b = 0;
 int result;
 try{
  result = a~/b;
 }
 on IntegerDivisionByZeroException {
  print('Cannot divide by zero');
 }finally{
  print('Finally block executed');
 }
}



c. Throwing an Exception

An execution exception can be raised explicitly by using the throw keyword. This exception must be handled by the programmer so that the program does not terminate abruptly.

void main(){
 void checkAge(int age) {
  if(age<){
     throw new FormatException();
  }
 }
 try{
   checkAge(-10);
 }catch(e){
   print('Age cannot be negative');
 }
}



Errors in Dart

An error in Dart is thrown for unexpected program flow. This should not be caught, but addressed by the programmer. An error denotes that the code may fail to run and can be fixed by the programmer.

a. What are errors in Dart?

The error occurs when there is a problem that is not expected. Some of the errors in Dart include syntax errors, null checks, the use of invalid functions and methods, and so on.

b. Difference between Errors and Exceptions:

The difference between errors and exceptions.


Errors:

  • The error will occur due to a lack of system resources.
  • Errors occurring at runtime are not known to the compiler.
  • Errors are unchecked.

Exceptions:

  • An exception will be caused by the application code itself.
  • They may or may not be caught by the compiler.
  • Exceptions can be checked or unchecked.


Dart Exceptions Exception Handling Erros try catch finally

advertisement