Dart Custom Exception Class example. Dart tutorial for Flutter. #7.2

preview_player
Показать описание

Dart beginners tutorial. Learn to create your own Custom Exception subclass of Exception. Use these classes as per your need in your application. So learn how to handle runtime errors using your own custom exception class.

.
Please donate and support my work
(If you think my free tutorials are better than paid ones :)

Free Programming courses:

Free Flutter course:

Free Android courses:

More free programming courses:

Check out my website:

Let's get in touch! [Sriyank Siddhartha]

---- Thank you for your love and support ----
Рекомендации по теме
Комментарии
Автор

great tutorial but doesn't work with latest dart version! I'm learning a lot from this tutorial.
i tried and manage to get it working as below codes, sharing & hope it can help others..


void main() {
print('case 5');
try {
depositMoney(-200);
} catch (e) {
print(e.toString());
}
}

class DepositException implements Exception {
@override
String toString() {
//return errorMessage();
return ('you cannot enter amounts < zero');
}

// method not needed, return text direct from overridden method toString()
// only need to override toString method from super class Exception to
//return error text
// return ('you cannot enter amount < 0');
// }
}

void depositMoney(int amount) {
if (amount < 0) {
throw DepositException();
}
}


Wongmc
Автор

Simple and clear. Your tutorials are very helpful. Thank you so much mate. Blessings at you 🙏🏼

RajA-mecl
Автор

Your tutorials are very helpful.
You're a legend.

rashedahmed
Автор

I think one important concept is missing here. You do not address how to differently handle different types of exceptions in multiple catch blocks corresponding to the same "try" also what happens if the custom exception caught does not implement the errorMessage( ) method as in your example (and if that's the case you will get noSuchMethod exception thrown from within the catch block).

try {
// code throwing different types of exceptions

} on DepositException catch (e) {
// a typed exception handled here, safe access to all methods and fields it actually implements/defines without the risk of noSuchMethod being thrown
} on SecurityException catch (e) {
// another exception type handled here, and similarly access to all methods and fields it actually implements/defines
}

hicnar
Автор

perfect! bro can u plz make an ebook of dart and flutter... its very helpful to refer stuffs quickly... thanks!:)

gaurav
Автор

class CustomException implements Exception {
String cause;
CustomException(this.cause);
String errorMessage() {
return cause;
}
}

void throwException(int amount) {
if (amount < 0) {
throw new CustomException("The amount less than zero not acceptable");
}
}

void main() {
try {
throwException(-200);
} on CustomException catch (e) {
print(e.errorMessage());
}
}

syednokhaizalhassan
Автор

FOR THOSE WHO ARE FACING PROBLEM WIITH NEW DARTPAD SDK

You can also implement toString() in the exception so you can call its message with e.toString().

so the code will be

void main(){

try{
depositMoney(-200);
} catch (e){
print(e.toString());
}
}

class DepositException implements Exception{
String toString() => "you cannot enter amount less than 0";
}

void depositMoney(int amount){
if(amount < 0){
throw DepositException();
}
}

//goodluck

vighneshsharma
Автор

hi @Smartherd, is there a way to make a custom Exception where i add only a string -that help to identify the place of exception on the program- to the existing Exception

MohamedDernoun
Автор

errormessage has no return value, can we use string type if there is no return statement??

ekshivbhakt
Автор

How do you accessed to depositMoney() from main method
Program says depositMoney() is not defined

ziasultan
Автор

Sir please help, in new update of Dart SDK V2.12.1 String errorMessage() is impeding the program to run, saying *The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. Try adding either a return or a throw statement at the end.*
Also it says _The method 'errorMessage' isn't defined for the type 'Object'. Try correcting the name to the name of an existing method, or defining a method named 'errorMessage'_ .

Edit :
_Actually in new version i.e. V2.12.1 and above, null string is no longer supported_ .
you can't do
*String* name ; // _initially null_
So now one has to write
*String* name = "name"; // _some default name initially that can be renamed later_

__shubham__
Автор

The example in the video gives errors with Dart version 3+

The new example:

void main() {
try {
depositMoney(-200);
} on DepositException catch (e) {
print(e.errorMessage());
}
}

class DepositException implements Exception {
String cause;
DepositException(this.cause);
String errorMessage() {
return cause;
}
}

void depositMoney(int amount) {
if (amount < 0) {
throw new DepositException("The amount less than zero not acceptable");
}
}

nolssmit
Автор

Sir... Please connect charger with your laptop... only 11% charge available. Thanks for your nice videos anyway:)

rifatmehedi
Автор

What is the difference in 'throw new' and just using 'throw'? Could you please help me with my confusion!

sushantprajapati
Автор

what is implement idon`t understanding this when you give name class and then class name idon't know where implements and exception comes from please help me

shaaficialli
Автор

You can also implement toString() in the exception so you can call its message with e.toString()
import

class InternalServerException implements Exception {
final String msg;


String errorMessage() {
return msg;
}

@override
String toString() => msg;
}

soonclass
Автор

you said at 1:15 Exception is a Class in dart
if it is a class then you should use extends keyword

as i know we use implements keyword with interface

please clear my doubt

abhaythakur
Автор

Unable to access errorMessage() from main(). It shows -
The method 'errorMessage' isn't defined for the type 'Object'.
Try correcting the name to the name of an existing method, or defining a method named 'errorMessage'.

shreyashkashyap
Автор

The code in this video unfortunately no longer seems to work

coffeeCatPeanutDust
Автор

I am trying a same code but in output show a error why?


Error: The method 'errorMessage' isn't defined for the class 'Object'.
- 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'errorMessage'.
print(e.errorMessage());

saad-rcmn