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

  Рет қаралды 49,359

Smartherd

Smartherd

Күн бұрын

Пікірлер: 58
@RajA-me9cl
@RajA-me9cl 4 жыл бұрын
Simple and clear. Your tutorials are very helpful. Thank you so much mate. Blessings at you 🙏🏼
@rashedahmed6339
@rashedahmed6339 2 жыл бұрын
Your tutorials are very helpful. You're a legend.
@gaurav1252
@gaurav1252 5 жыл бұрын
perfect! bro can u plz make an ebook of dart and flutter... its very helpful to refer stuffs quickly... thanks!:)
@hicnar
@hicnar 5 жыл бұрын
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 }
@zeusking4468
@zeusking4468 Жыл бұрын
correct !
@Wongmc644
@Wongmc644 2 жыл бұрын
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(); } } ==============
@vighneshsharma
@vighneshsharma 2 жыл бұрын
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
@jayednahian1405
@jayednahian1405 2 жыл бұрын
thats nice without using the specific function name ! i came up with a conclusion too !.. try { depositMoney(-200); } catch (e) { print(DepositException().errorMessage()) }
@labeeb2903
@labeeb2903 2 жыл бұрын
try{ depositAmount(-100); } on DepositException catch (e){ print(e.errorMessage()); }
@eainsworld4864
@eainsworld4864 Жыл бұрын
Thank you so much.
@gamersguildboxbreaks9063
@gamersguildboxbreaks9063 2 ай бұрын
I literally don't understand but it worked Pls can I get your Instagram or WhatsApp number so you can explain better to me pls Thanks
@soonclass4269
@soonclass4269 5 жыл бұрын
You can also implement toString() in the exception so you can call its message with e.toString() import 'package:flutter/material.dart'; class InternalServerException implements Exception { final String msg; InternalServerException({@required this.msg}); String errorMessage() { return msg; } @override String toString() => msg; }
@syednokhaizalhassan1800
@syednokhaizalhassan1800 3 жыл бұрын
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()); } }
@rifatmehedi2082
@rifatmehedi2082 5 жыл бұрын
Sir... Please connect charger with your laptop... only 11% charge available. Thanks for your nice videos anyway:)
@smartherd
@smartherd 5 жыл бұрын
Haha
@rahulsrinivas1004
@rahulsrinivas1004 4 жыл бұрын
chutiya
@saisasisai
@saisasisai 4 жыл бұрын
Nice 👌 Tutorial series ...!
@abhaythakur8572
@abhaythakur8572 5 жыл бұрын
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
@__shubham__
@__shubham__ 3 жыл бұрын
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_
@shreyashkashyap
@shreyashkashyap 2 жыл бұрын
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'.
@mayankgarg9728
@mayankgarg9728 2 жыл бұрын
yes please tell; and the error is logically right too as without creating an object of class how can you directly call its function?
@jayednahian1405
@jayednahian1405 2 жыл бұрын
@@mayankgarg9728 i think " throw new DepositException();" line of helps to create the object
@parikumari7004
@parikumari7004 2 жыл бұрын
You have solution for this question, i am facing same problem plzz help?
@subhamkrpaul1034
@subhamkrpaul1034 2 жыл бұрын
Try this code, Happy coding: void main() { try { throwException(-200); } on CustomException catch (e) { print(e.errorMessage()); } } class CustomException implements Exception { String cause; CustomException(this.cause); String errorMessage() { return cause; } } void throwException(int amount) { if (amount < 0) { throw CustomException("The amount less than zero not acceptable"); } }
@psychopath4351
@psychopath4351 2 жыл бұрын
thanks bro. you saved my day!
@ripudhman7355
@ripudhman7355 Жыл бұрын
Thanks 🙏
@MohamedDernoun
@MohamedDernoun 4 жыл бұрын
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
@nolssmit
@nolssmit Жыл бұрын
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"); } }
@redpersimmon6940
@redpersimmon6940 Жыл бұрын
Thankyou So Much 👍
@muazbhatti41
@muazbhatti41 3 жыл бұрын
great information in this section.
@shaaficialli4284
@shaaficialli4284 4 жыл бұрын
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
@AztechZone1
@AztechZone1 3 жыл бұрын
indeed it is an intersting lecture
@sushantprajapati1704
@sushantprajapati1704 5 жыл бұрын
What is the difference in 'throw new' and just using 'throw'? Could you please help me with my confusion!
@vibhor1386
@vibhor1386 2 жыл бұрын
what's rethrow?
@ekshivbhakt2417
@ekshivbhakt2417 6 жыл бұрын
errormessage has no return value, can we use string type if there is no return statement??
@ziasultan8157
@ziasultan8157 5 жыл бұрын
How do you accessed to depositMoney() from main method Program says depositMoney() is not defined
@saad-rc8mn
@saad-rc8mn 3 жыл бұрын
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());
@Anonymous-ig5nm
@Anonymous-ig5nm 3 жыл бұрын
Same for me also how to debug it
@dibyodhara4326
@dibyodhara4326 2 жыл бұрын
try { depositMoney(-200); } on DepositException catch(e) { print(e.errorMessage()); }
@parikumari7004
@parikumari7004 2 жыл бұрын
@@dibyodhara4326 nice , thanks
@ritwikrajsingh3420
@ritwikrajsingh3420 3 жыл бұрын
Few things are outdated now, please try to fix them, it'll help alot
@thefossenjoyer3346
@thefossenjoyer3346 2 жыл бұрын
Yeah, this is outdated. Weird how much a language can change in 4 years. xD
@ritwikrajsingh3420
@ritwikrajsingh3420 2 жыл бұрын
@@thefossenjoyer3346 Google is trying to make a mess instead of convenience.
@thefossenjoyer3346
@thefossenjoyer3346 2 жыл бұрын
@@ritwikrajsingh3420 ah yes.
@arnabdeb1486
@arnabdeb1486 3 жыл бұрын
dartpad shows the method 'errorMessage' isn't defined for the type object
@dineshbhavsar1435
@dineshbhavsar1435 3 жыл бұрын
Same Daut me
@georgerassovsky3733
@georgerassovsky3733 3 жыл бұрын
Had the same problem. This works for me: try { depositMoney(-200); } on DepositException catch (e) { print(e.errorMessage()); }
@davestr2046
@davestr2046 6 жыл бұрын
Perfect!
@cybernerd7492
@cybernerd7492 3 жыл бұрын
nice vid
@coffeeCatPeanutDust
@coffeeCatPeanutDust 2 жыл бұрын
The code in this video unfortunately no longer seems to work
@raptorrogue4227
@raptorrogue4227 8 ай бұрын
This doesnt work on dartpad 😓
@bivu8951
@bivu8951 6 жыл бұрын
Cant understand -- "implements Exception"
@smartherd
@smartherd 6 жыл бұрын
Continue watching. U will get to know what it is
@jesssinues415
@jesssinues415 6 жыл бұрын
@0:32 Thank me later.
@md.soleymankhan6550
@md.soleymankhan6550 3 жыл бұрын
Do not know why that code is not running. But this worked: class DepositException implements Exception{ String errorMessage() => "you can not add amount less then 0"; } void main(){ int amount = -200; try { depositMoney(amount); }catch(e){ print(e.errorMessage()); } } void depositMoney(int amount){ if(amount
@kapilsahu1218
@kapilsahu1218 6 жыл бұрын
noob! :p
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Catch Exceptions in Flutter Like Never Before!
9:02
Rivaan Ranawat
Рет қаралды 22 М.
Java Custom Exceptions Tutorial - It's Way Easier Than You Think
14:29
Coding with John
Рет қаралды 171 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 93 М.
STOP Learning These Programming Languages (for Beginners)
5:25
Andy Sterkowitz
Рет қаралды 734 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41