How to catch division by zero in Dart

4 Answers

0 votes
void main() { 
   int x = 19; 
   int y = 0; 
   int result;  
   
   try {
      result = x ~/ y; 
   } 
   on IntegerDivisionByZeroException { 
      print('Error: divide by zero'); 
   } 
} 




/*
run:

Error: divide by zero

*/

 



answered Nov 3, 2022 by avibootz
0 votes
void main() { 
   int x = 19; 
   int y = 0; 
   int result;  
   
   try {
      result = x ~/ y; 
   } 
   catch(e) { 
      print(e); 
   } 
} 




/*
run:

IntegerDivisionByZeroException

*/

 



answered Nov 3, 2022 by avibootz
0 votes
void main() { 
    int x = 19; 
    int y = 0; 
    int result;  
   
    try {
      result = x ~/ y; 
    } 
    on IntegerDivisionByZeroException catch(e) { 
        print(e); 
    } 
} 




/*
run:

IntegerDivisionByZeroException

*/

 



answered Nov 3, 2022 by avibootz
0 votes
void main() { 
    int x = 19; 
    int y = 0; 
    int result;  
   
    try {
      result = x ~/ y; 
    } 
    on IntegerDivisionByZeroException { 
      print('Error: divide by zero'); 
    } 
    finally { 
      print('Finally'); 
   } 
} 




/*
run:

Error: divide by zero
Finally

*/

 



answered Nov 3, 2022 by avibootz

Related questions

1 answer 139 views
139 views asked Oct 8, 2022 by avibootz
1 answer 134 views
134 views asked Oct 8, 2022 by avibootz
1 answer 133 views
133 views asked Oct 8, 2022 by avibootz
3 answers 239 views
239 views asked Mar 25, 2023 by avibootz
1 answer 176 views
1 answer 163 views
...