[Learning] Dart - Dart of exception handling

  Overview:

    Dart2 exception and Java are very similar. Dart2 exception is Exception or Error (including their sub-class) type, or even a non-Exception Error class, also can throw, but not recommended for such use.

  Exception mainly abnormal program itself can handle, such as: IOException. This is an exception to an exception mainly we deal with.

  Error wrong program can not handle, indicate more serious problems running the application. Most errors unrelated to the operation and execution of code writers, and that the problem appears DartVM code runs. For example: out of memory (OutOfMemoryError) and so on.

  The difference is that with Java, Dart2 not detect whether an abnormality statement, that the method or function which does not need to declare an exception to be thrown.

  • Throw an exception

    Use throw an exception is thrown, an exception can be Excetpion or Error type may also be other types, but it is not recommended to use it. Further, the throw statement is also a Dart2 expression, thus may be =>.

    // non Exception or Error type is thrown, so it is not recommended to use

    testException(){
      throw "this is exception";
    }
    testException2(){
      throw Exception("this is exception");
    }

    // can also be used =>

    void testException3() => throw Exception("test exception");
  • Catch the exception

    • on can capture a certain type of abnormal, but not obtain the exception object;
    • catch can capture the exception object. The two keywords can be used in combination.
    • rethrow can be re-thrown capture.

      testException () {
        the throw a FormatException ( " the this Exception IS " ); 
      } 
      
      main (List <String> args) {
       the try { 
           testException (); 
         } ON a FormatException the catch (E) { // If a match is not FormatException, will continue to match 
            Print ( " the catch exception the format " ); 
            Print (E); 
            the rethrow; // rethrows 
         } ON exception { // not match exception, will continue to match 
            Print ( " the catch exception " ); 
        } the catch(e, R & lt) { // . matches the type of anomaly so that the object is an abnormal e, r is StackTrace object exception stack information 
            Print (e); 
        } 
      }
  • finally

    • Inside the finally statement, regardless of whether there is an exception, it will be executed.
      testException(){
         throw FormatException("this is exception");
      }
      
      main(List<String> args) {
       try{
         testException();
        } on FormatException catch(e){
         print("catch format exception");
         print(e);
         rethrow;
        } on Exception{
         print("catch exception") ;
        }catch(e, r){ 
         print(e);
        }finally{
         print("this is finally" ); // performed before the rethrow 
        } 
      }

       

 

Guess you like

Origin www.cnblogs.com/lxlx1798/p/11025046.html