try… on…
Relation specific exception, for different exceptions for different processing
Main (List<String> args){try{// print(11~/0); } on IntegerDivisionByZeeroException {print (" divisor to 0 "); } on Exception{ print("Exception"); } finally{ print("finally"); }}Copy the code
You want different handling for different exceptions, and you want to print the call stack information
main(List<String> args){ try{ print(11~/0); } on IntegerDivisonByZeroException catch(e,s){ print(s); } on Exception catch(e,s){ print(s); }}Copy the code
try… catch…
You don’t care about specific exceptions; you just want to catch them and prevent them from propagating
main (List<String> args){ try{ print(11~/0); } catch(e){print(e); } finally{ print("finally"); }}Copy the code
To get more information about an exception, you can use a two-parameter catch, the second parameter being the call stack information for the exception.
main(List<String> args){ try{ print("11~/0"); } catch(e,s){ print(s); }}Copy the code