Introduction

Exception is an abnormal situation in the program. In JAVA, there are checked Exception and unchecked Exception. So is the situation the same in dart? Let's take a look.

Exception and Error

There are two classes representing exceptions in Dart, namely Exception and Error. What is the difference between the two of them?

Exception is thrown by VM or dart code.

Exception is mainly used to indicate the exceptions generated in the process of writing user programs, which can be located and resolved. Generally speaking, the Exception contains enough information to facilitate the user to locate the abnormal point.

So Exception usually needs to be caught. But unlike java, all exceptions in dart are unchecked exceptions, which means that exceptions in dart are not mandatory to be caught, and whether to catch exceptions is determined by the programmer.

Constructing an exception is very simple, as shown below:

Exception("message")

But dart does not recommend such use, because the exceptions constructed in this way are too general, and even if such exceptions are caught, less information can be obtained. Therefore, dart recommends throwing custom exceptions, that is to say, create Exception corresponding classes according to business needs, and then throw them according to business needs.

There are also many subclasses of Exception in dart, such as FormatException to represent various abnormal situations.

Similarly, it is also recommended in JAVA. Don't throw Exception directly, but throw custom exceptions according to business needs.

Like JAVA, Error in dart represents a serious error, and Error should be avoided in the process of programming.

The Error in dart does not need to be caught, because an Error indicates that the program has a very serious error and can no longer run.

So Error is what we need to avoid in the process of programming.

Throw and catch

If the program generates an exception, you can use the Throw statement to throw it, and then use catch to catch it where appropriate.

For example, we throw a format exception:

throw FormatException('这是一个格式异常');

But in dart, not only can throw Exception or Error, any Object can be thrown, as shown below:

throw "这是一个异常!";

The thrown exception can be caught using catch:

try{
    do something
}catch(e){

}

Dart can also catch specific exceptions. This situation is represented by an on statement, as follows:

try {
  someException();
} on OutOfIndexException {
  // 捕获特定的异常
  doSomething();
} on Exception catch (e) {
  // 捕获其他的Exception
  print('其他的异常: $e');
} catch (e) {
  // 处理剩下的异常
  print('剩下的异常: $e');
}

The catch in dart can specify two parameters. The first parameter is the exception of throw, and the second parameter is the StackTrace object:

try {
} catch (e, s) {
  print('异常信息: $e');
  print('堆栈信息: $s');
}

After handling the exception, if you want to throw it again, you can use rethrow:

void doSomething(){
    try{
    }catch (e) {
    print('get exception');
    rethrow; // rethrow这个异常
  }
}

Finally

Like JAVA, there is Finally in dart for final processing. Finally will be executed after all the catch statements are executed:

try {
  doSomething();
} catch (e) {
  print('Error: $e'); 
} finally {
  cleanUpJob(); // 最后的清理工作
}

Summarize

The above is the exception in dart and the handling of exceptions.

This article has been included in http://www.flydean.com/05-dart-exception/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com