How to throw an exception in Dart

2 Answers

0 votes
main() {
    try {
        throw "throw exception";
    } catch (e) {
        print("Exception: $e");
    }
}




/*
run:

Exception: throw exception

*/

 



answered Oct 21, 2022 by avibootz
0 votes
void main() { 
   try { 
      test(-7); 
   } 
   catch(e) { 
      print('Number cannot be negative'); 
   } 
}  
void test(int n) { 
   if(n < 0) { 
      throw new FormatException(); 
   } 
}




/*
run:

Number cannot be negative

*/

 



answered Nov 3, 2022 by avibootz

Related questions

1 answer 121 views
121 views asked Oct 21, 2022 by avibootz
1 answer 144 views
144 views asked Oct 8, 2022 by avibootz
1 answer 147 views
1 answer 179 views
1 answer 187 views
1 answer 178 views
...