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 116 views
116 views asked Oct 21, 2022 by avibootz
1 answer 141 views
141 views asked Oct 8, 2022 by avibootz
1 answer 142 views
1 answer 164 views
1 answer 183 views
1 answer 173 views
...