In dart, we can handle exceptions. We see errors can’t be solved.

Let’s discuss about keywords of exception handling –

* try block – try block contains program statements that are to be to monitored for exceptions, If an exception occurs within the try block, it is thrown

• catch block –contain statements that catch this exception and handle it in some rational manner, System-generated exceptions are automatically thrown by the Dart runtime system.

throw –is used to manually throw an exception

finally –block contains code that absolutely must be executed after a try block completes

Exceptions in dart while programming is basically bifurcated into two categories such as:

Build-In Exceptions: These are the types of exception which can be caught using already existing dart libraries. It is also known as Unchecked Exception or Runtime Exception.

Some of the built-in exceptions are –

  • DeferredLoadException
  • FormatExceptionIntegerDivisionByZeroException
  • IOExceptionIsolateSpawnException
  • NullRejectionExceptionOSErrorTimeoutException

Etc.

Try-catch block

Let’s implement try-catch block for handling exception

code 1:

void main(){
  late int a;
  try{
    a=10%0;
  }
  catch(e){
    print(e);
  }
  print(‘statement after try-catch block!’);
}
output

Finally block

Block contains code that absolutely must be executed after a try block completes. Even though try-catch block not executed but this block will definitely executed.

code 2:

import ‘dart:io’;
void main(){
  try{
    int a=10%0;
  }
  // exception type -> IntegerDivisionByZeroException
  on IOException catch(e){
    print(e);
  }
  finally{
    print(‘finally block executed!!’);
  }
}
output

User-defined Exception

This exception type created by user (programmer).

These are the types of exception which can be caught using some of the customized exception created by the user and the user should have the ability to handle these exceptions. These exceptions can also be called as Checked Exception or Compile time exception.

Let’s see uses of try-catch block for exception handling –

Using try and catch

• Exception handling benefits

– allows you to fix the error

– prevents the program from automatically terminating

Let’s see user-defined exception…

Code 3:

class CustomException implements Exception {
  String cause;
  CustomException(this.cause);
}
 
void main() {
  try {
    throwException();
  } on CustomException {
    print(“custom exception has been obtained”);
  }
}
 
throwException() {
  throw new CustomException(‘This is my first custom exception’);
}
output

code 4:

import ‘dart:math’;
 
/**
 * When it comes to throwing exception manually, Dart provide some built-in classes.
 * An exception class implements built-in `Exception` class.
 *
 * To throw an custom exception, we need to create a custom class that implements `Exception` class.
 */
void main() {
 
  try {
   
    var result = squareRoot(-4);
   
    print( “result: $result” );
   
  } on NegativeSqrtException catch( e ) {
   
    print( “Oops, Negative Number: $e” );
   
  } catch( e ) {
   
    print( e );
   
  } finally {
   
    print( ‘Job Done!’ );
   
  }
}
 
// custom exception class
class NegativeSqrtException implements Exception {
  @override
  String toString() {
   
    return ‘Sqauare root of negative number is not allowed.’;
   
  }
}
 
// get square root of a positive number
num squareRoot( int i ) {
  if( i < 0 ) {
   
    // throw `NegativeSqrtException` exception
    throw NegativeSqrtException();
   
  } else {
   
    return sqrt( i );
   
  }
}
output