Dart Exception Handling

Add Your Heading Text Here

Exception in a programming language is a termination of the program during execution.

Exception is an unwanted or unexpected event which interrupts the normal flow of the program execution.

Exception must be taken care to continue our program execution. Otherwise program or application gets terminated.

For ex :

void main(){
  	List games=new List();
	print("Exception Test");
	// Index out of range exception will occur due to no element will be found in the list at index as list is empty. Hence, further statements will not executed with the termination of the program.
  	print(games[5]);
	print("Exception Test");
}

Whenever an exception occurs, program gets terminated abnormally without executing remaining part of the program. That’s why we have to handle the exception.

What is Exception Handling?

Exception handling is a mechanism to solve or handle the runtime errors. There are different kinds of exceptions can strike based on which type of mistake you have made while writing your program. Like, IndexOutOfRangeException, IOException, NullPointerException etc.

Exception handling is the mechanism to handle unknown mistakes you made in your program to prevent program or application termination.

Dart have some concepts for handling exceptions smoothly as following,

  • try
  • on/catch
  • finally
  • throw

try

Block of code or statements which might result in an exception must be kept inside try block to throw generated exception.

Generated exception will be thrown to the on or catch block.

Syntax :

try{
            // block of code
}catch(e){
            // handle thrown exception
}

Try block must be followed by on or catch or finally block.

on/catch

On or catch block are used to handle the thrown exception from try block.

On or catch keywords can be used separately as well as together to catch an exception.

A try block can be followed by multiple catch blocks. Multiple catch blocks are used to handle different types of exceptions.

Syntax :

try{
            // code block
}
// to use on keyword it must be followed by the Exception that may occur. On should be used only when you definitely know that particular exception will occur from try block. Otherwise, catch will always handle your exceptions smoothly.
on ExceptionName catch(e){
            // code block
}

In above syntax, ExceptionName is the type of exception that may occur based on different situations. Like, ArithmaticException, IntegerDivisionByZeroException etc. can be thrown via try block if you made some mistake related to them.

finally

finally is a block used to perform certain operations whether exceptions take place in program or not.

finally is generally used to release the different resources requested during the operation. In general case, releasing database resources, closing opened files etc.

Syntax :

try{

}finally{
            //statements you want to perform
}

finally must have an associate try block.

For ex :

void main(){  
  try{
     List<String> games=new List();
    print(games[5]);
  }on IntegerDivisionByZeroException catch(e){
    print("Divide By Zero Error");
  }on IndexError catch(e){
    print("Handle Index Error");
  }finally{
    print("Release Resources.");
  }
}

Output :

Handle Index Error
Release Resources.

Throw

Throw keyword is used to manually throw the exception in programs.

Generally, it is used to terminate the program while some logical condition does not satisfies as per our requirement.

Syntax :

throw new ExceptionName();

Here ExceptionName is the exception you want to be thrown to terminate the program.

To solve the program termination due to exceptions thrown using throw keyword, use throw in try-catch block.

For ex :

void main(){
  int x=5,y=0;
  if(y==0){
	// throwing an exception
     throw new IntegerDivisionByZeroException();
  }else{
    print(x);
  } 
}

Output :

Uncaught exception: IntegerDivisionByZeroException

Mistakes can happen by any one. So exception can strike at any point in your program and the solution of our problem is exception handling. Hence, we have learned exceptions to take care our programs for better user experience.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons