dart try-catch

try {
  // ...
} on SomeException catch(e) {
 //Handle exception of type SomeException
} catch(e) {
 //Handle all other exceptions
}

4.5
2
Krish 100200 points

                                    try {
  breedMoreLlamas();
} on OutOfLlamasException {			// A specific exception  
  buyMoreLlamas();
} on Exception catch (e) { 			// Anything else that is an exception
  print('Unknown exception: $e');
} catch (e) {						// No specified type, handles all
  print('Something really unknown: $e');
} finally {							// Always clean up, even if case of exception
  cleanLlamaStalls();
}

4.5 (2 Votes)
0
3.89
9
Awgiedawgie 440215 points

                                    
HOW TO HANDLE EXCEPTIONS IN DART/FLUTTER!

# ------------------------ CASE 1: --------------------------- #
When you know the exception to be thrown, use ON Clause
	try {
		int result = 12 ~/ 0;
		print("The result is $result");
	} on IntegerDivisionByZeroException {
		print("Cannot divide by Zero");
	}

# ------------------------ CASE 2: --------------------------- #
When you do not know the exception use CATCH Clause

	try {
		int result = 12 ~/ 0;
		print("The result is $result");
	} catch (e) {
		print("The exception thrown is $e");   
	}

# ------------------------ CASE 3: --------------------------- #
Using STACK TRACE to know the events that occurred before the
Exception was thrown (trace and print the code steps after the 
error)

	try {
		int result = 12 ~/ 0;
		print("The result is $result");
	} catch (e, s) {
		print("The exception thrown is $e");
		print("STACK TRACE \n $s");
	}

# ------------------------ CASE 4: --------------------------- #
Whether there is an Exception or not, FINALLY Clause is always 
Executed

	try {
		int result = 12 ~/ 3;
		print("The result is $result");
	} catch (e) {
		print("The exception thrown is $e");
	} finally {
		print("This is FINALLY Clause and is always executed.");
	}

# ------------------------ CASE 5: --------------------------- #
Custom Exception.The throw keyword is used to explicitly raise 
an exception, and, in this case, that exception was defined by 
the following exemple: 

void main(){

	try {
		depositMoney(-200);
	} catch (e) {
		print(e.errorMessage());
	} finally {
		// Code
	}
}

class DepositException implements Exception {
	String errorMessage() {
		return "You cannot enter amount less than 0";
	}
}

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

3.89 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
dart try catch go to catch dart catch try flutter try-catch exception try catch finally flutter exception dart dart uncaught exception in try catch define exception in dart what is an Exception type in dart dart force to use try catch how to implement Exception in dart dart exceptions using try and catch in dart dart on catch Exception in Dart return in try catch block dart try catch not getting exception dart try except dart dart try within try exception class flutter dart try catch error try catch different flutter how to check exception type check in flutter dart catch exception from future dart error handling catch errors dart exception in flutter try catch exception dart try catch flutter example dart try catch shortcut try catch null dart exceptions dart dart http exception catch block in dart fluter try catch dart try on error exception handling in dart flutter flutter try catch exception flutter check exception type flutter exception dart try catch e, make exception handling in dart how to catch exceptions in flutter dart exceptions in dart dart exception where and how to use try catch methods in flutter how to use try catch in flutter exception handling in dart dart try return flutter try on catch try catch finally in dart try catch future flutter flutter try catch Dart try catch and complete flutter try and catch try exept dart dart test try catch block dart try catcher check exception type flutter Dart try and catch dart catch on exception try catch except flutter try and catch dart dart trycathc dart try error dart try catch on error try catch example flutter dart catch error dart try finally dart on try catch dart inline try catch try then catch in flutter dart example try cath dart Try-catch in Dart try and catch in dart dart try catch syntax do catch dart dart trycatch dart try catch exception message catch exception dart exception handling dart try dart try on catch dart how to write try catch in flutter try flutter dart catch exceptions dart try on catch dart try catch insinde try catch throe excebtion flutter try and catch flutter dart try catch exception catchError in dart try catch block in dart throw Exception fluttwr dart future catcherror throw exception on flutter how to use catchError in flutter try catch block on dart future tryon flutter flutter try catch finally italiano dart excepiton handling dart .catchError dart print error Future<Either<Failure,Success>> flutter where to place the try catch block in dart dart try catch future dart throw errors try catch continue dart whenever I throw an exception dart dart catch exception type dart raise error how to handle exception dart future.error dart dart catch on dart try catch example what does throw in dart language explicitly throw error in dart dart try catch error type dart try catch async how to throw an exception in flutter dart async await whenCompleted dart catch flutter future then catcherror flutter then catcherror flutter future catcherror flutter whencomplete catcherror dart future error type try on dart flutter dart try catch all exceptions try on dart catch dart how to get exception type in dart how throw exception dart flutter catcherror await dart how to throw an object if an error occurs network future error handling future error handling dart throw exception flutter throw error dart try catch on try cathc dart flutter try catch finally dart then catch block entire dart file throws errors catch specific exception dart dart try-catch dart get error throw exception in flutter try catch finally dart how to throw error with message in dart dart try catch flutter' dart throw try catch dart any exception try catch dart flutter throw exception dart flutter try catch dart$ flutter tfinally flutter try finally dart try catch then block dart try catch success block try catach dart dart return value from try catch try and catch with if dart try catch example in flutter then catch finally flutter flutter try catch error catch multiple exceptions dart flutter try on finally throw an error dart throw exception flutter catch on then dart throw exception flutter exception handling dart throw exception when call database dart custom errror handler dart throw error with message catch in dart try catch in flutter flutter try dart show error message dart catch all exceptions flutter throw exception try catch in dart try catch flutter flutter try catch dart catch exception howto know what execeptions to catch dart dart throw new exception on catch dart dart try catch get exceptio object flutter throw exception with message flutter try catch dart trycatcgh dart catch for evry exeception dart flutter try catch example catch error dart try dart erro dart throw error dart raise exception try catch dart dart exception handling tey except dart dart try catch finally Flutter Caught error dart try catch dart throw new error dart try except throw catch dart dart try
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source