Professional Documents
Culture Documents
Day5-2-Exceptions - 2
hours
Dart Exceptions are the run-time error. It is raised when the program gets
execution. The program doesn't report the error at compile time when the
program runs internally and if Dart compiler found something not
appropriate. Then, it reports run-time error and the execution of program
is terminated abnormally. This type of error is called Exceptions. For
example - A given number is divided by the zero or we try to access the
elements from the empty list.
Dart supports the following types of built-in exceptions.
Show All
Built-in Exceptions
Exceptions
DefferedLoadException
FromatException
IntegerDivisionByZeroException
IOEException
IsolateSpawnException
Timeout
1 of 15 10/11/23, 12:36 AM
Day5-2-Exceptions - 2 hours https://devnation.notion.site/Day5-2-Exceptions-2-hours-392303065d4...
The main objective of the exception is to handle the run-time error and
prevent the program from terminating abruptly. Every exception in the
Dart is a subtype of the pre-defined class Exception. Dart provides the
following techniques to handle the exceptions.
Syntax:
• We can handle the multiple exceptions using the more than one catch
block.
• The on block and the catch block is mutually inclusive that means we
can associate the both - the on block and catch block with the try
block.
In the following example, the variable x is divided by the y variable
respectively. The code is thrown when it tries to divide by the zero. The on
block consists of the code to handle the exception. Let's understand the
following code.
2 of 15 10/11/23, 12:36 AM
Day5-2-Exceptions - 2 hours https://devnation.notion.site/Day5-2-Exceptions-2-hours-392303065d4...
Output
Cannot divide by zero
Explanation:
In the above code, we declared the three variable x, y and res in main
() function. We written the suspect code in try block divided the x by the 0
that might be thrown an exception. The try block found the error the
control transferred to the on block that has the code to handle the error.
By using this, the program did not stop its execution.
Let's understand the following example using the catch block.
Output
IntegerDivisionByZeroException
Output
IntegerDivisionByZeroException
3 of 15 10/11/23, 12:36 AM
Day5-2-Exceptions - 2 hours https://devnation.notion.site/Day5-2-Exceptions-2-hours-392303065d4...
IntegerDivisionByZeroException
Copy the above code and paste it into DartPad below to practice:
Syntax -
4 of 15 10/11/23, 12:36 AM
Day5-2-Exceptions - 2 hours https://devnation.notion.site/Day5-2-Exceptions-2-hours-392303065d4...
Example -
Output
Cannot divide by zero
Finally block executed
Copy the above code and paste it into DartPad below to practice:
5 of 15 10/11/23, 12:36 AM
Day5-2-Exceptions - 2 hours https://devnation.notion.site/Day5-2-Exceptions-2-hours-392303065d4...
Throwing an Exception
We can raise an exception explicitly or forcefully. The explicitly raised
exception should be handled to avoid the program from existing sharply.
The syntax is given below.
Syntax:
Example -
Output
The marks cannot be negative
Custom Exceptions
As we discussed above, each of the exception in dart is the subtype of the
built-in class Exception. Dart provide the flexibility to create custom
exception by extending the existing exception class. The syntax is given
below.
6 of 15 10/11/23, 12:36 AM
Day5-2-Exceptions - 2 hours https://devnation.notion.site/Day5-2-Exceptions-2-hours-392303065d4...
Example -
Output
Entered Amount should be greater than zero
Ending requested operation.....
Explanation:
In the above example, we created a custom exception, AmtException. The
code raised the exception if the entered amount is not within the
excepted range and we enclosed the function invocation in the try…catch
block.
Copy the above code and paste it into DartPad below to practice:
7 of 15 10/11/23, 12:36 AM