<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用 Go 語言異常機制模擬 TryCatch 異常捕捉

          共 5883字,需瀏覽 12分鐘

           ·

          2019-05-21 17:34

          ? ? ? 有的同學看到Go和TryCatch一起出現,心里可能會說,難道Go語言升級了,加入了try...catch語句。哈哈,其實Go語言從創(chuàng)建之初就沒打算加入try...catch語句,因為創(chuàng)建Go的那幫大爺認為try...catch挺煩人的,如果濫用,會造成程序混亂,所以就不打算加入try...catch(以后加不加入不好說)。

          ? ? ? 既然Go語言中并沒有try...catch語句,那么為何文章標題說要使用TryCatch呢?其實Go語言中只是沒有try...catch語句,并不是沒有異常處理機制。Go語言中的異常處理機制就是著名的異常三劍客:panic、defer和recover。通過這3個家伙,是完全可以模擬出try...catch語句效果的,對了,后面還應該有個finally。在正式模擬try...catch語句之前,先來回顧下Go語言中的異常處理機制是如何玩的。

          Go語言中的異常處理機制

          在前面提到,Go語言通過panic、defer和recover來處理異常的,那么這3個東西是什么呢?

          不管是什么異常處理機制,核心的原理都是一樣的,通常來講,一個完善的異常處理機制需要由下面3部分組成。

          • 拋出異常
          • 處理異常的代碼段
          • 獲取異常信息

          下面先用Java的異常處理機制來說明這一點。

          ?

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          import?java.io.IOException;
          ?
          public?class?Main {
          ?
          ????public?static?void?main(String[] args) {
          ????????try
          ????????{
          ????????????boolean?ioException =?false;
          ????????????if?(ioException) {
          ????????????????throw?new?IOException("ioexception");
          ????????????}?else?{
          ????????????????throw?new?Exception("exception");
          ????????????}
          ????????}
          ????????catch?(IOException e) {
          ????????????System.err.println(e);
          ????????}
          ????????catch?(Exception e) {
          ????????????System.out.println(e);
          ????????}
          ????????finally
          ????????{
          ????????????System.out.println("finally");
          ????????}
          ????}
          }

            

          ? ? ? 上面的代碼是標準的Java異常處理機制,try部分的throw用于拋出異常,而catch部分的代碼段用于處理特定的異常,通過catch子句的參數e可以獲取異常信息。所以對于Java來說,上述的3個異常重要的組成部分都有。

          ? ? ? 對于Go語言來說,panic、defer和recover也分別對應了這3部分。其中panic是一個函數,用于拋出異常,相當于Java中的throw函數。defer是一個關鍵字,用于修飾函數,用defer修飾的函數,在拋出異常時會自動調用。recover是一個函數,用于獲取異常信息,通常在用defer修飾的函數中使用。

          ? ? ?下面是一段用Go語言處理異常的代碼。

          ?

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          package?main
          ?
          import?"fmt"
          ?
          func?main(){
          ????//? 處理異常的函數
          ????defer?func(){
          ????????fmt.Println("開始處理異常")
          ????????// 獲取異常信息
          ????????if?err:=recover();err!=nil{
          ????????????//? 輸出異常信息
          ????????????fmt.Println("error:",err)
          ????????}
          ????????fmt.Println("結束異常處理")
          ????}()
          ????exceptionFun()
          }
          ?
          func?exceptionFun(){
          ????fmt.Println("exceptionFun開始執(zhí)行")
          ????panic("異常信息")
          ????fmt.Println("exceptionFun執(zhí)行結束")
          }

            

          實現Go版的TryCatch

          現在已經了解了Go語言的異常處理機制,那么接下來使用異常處理機制來模擬try...catch...finally語句。

          現在來分析一下如果模擬。模擬的過程需要完成下面的工作。

          • try、catch和finally這3部分都有各自的代碼段,所以為了模擬try...catch...finally,需要用3個Go函數來分別模擬try、catch和finally部分的代碼段。這3個Go函數是Try、Catch和Finally。
          • 要確定這3個函數在什么地方調用。Try是正常執(zhí)行的代碼,所以在要首先調用Try函數。而Catch函數只有在拋出異常時調用,所以應該在用defer修飾的函數中調用,而且需要在Catch函數中獲取異常信息,所以應該在使用cover函數獲取異常信息后再調用Catch函數,通常會將異常信息直接作為參數傳遞給Catch函數。不管是否拋出異常,Finally函數都必須調用,所以應該用defer修飾Finally函數,而且是第1個用defer修飾的函數。這樣,在當前函數結束之前一定剛回調用Finally函數。
          • 觸發(fā)異常,這就非常簡單了,直接用panic函數即可。

          上面清楚地描述了用Go語言的異常處理機制模擬try...catch...finally語句的基本原理,下面給出完整的實現代碼。

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          package?main
          import?(
          "fmt"
          )
          type?ExceptionStruct?struct?{
          ????Try?????func()
          ????Catch???func(Exception)
          ????Finally?func()
          }
          type?Exception?interface{}
          func?Throw(up Exception) {
          ????panic(up)
          }
          func?(this ExceptionStruct) Do() {
          ????if?this.Finally != nil {
          ?
          ????????defer?this.Finally()
          ????}
          ????if?this.Catch != nil {
          ????????defer?func() {
          ????????????if?e := recover(); e != nil {
          ????????????????this.Catch(e)
          ????????????}
          ????????}()
          ????}
          ????this.Try()
          }
          ?
          func?main() {
          ????fmt.Println("開始執(zhí)行...")
          ????ExceptionStruct{
          ????????Try:?func() {
          ????????????fmt.Println("try...")
          ????????????Throw("發(fā)生了錯誤")
          ????????},
          ????????Catch:?func(e Exception) {
          ????????????fmt.Printf("exception %v\n", e)
          ????????},
          ????????Finally:?func() {
          ????????????fmt.Println("Finally...")
          ????????},
          ????}.Do()
          ????fmt.Println("結束運行")
          }

            

          上面的代碼將Try、Catch、Finally函數都封裝在了ExceptionStruct結構體中。然后調用方式就與前面的描述的一致了。執(zhí)行這段代碼,會輸出如下圖的信息。

          ?

          1.png?

          增強版的TryCatch

          ? ? ? ?到現在為止,其實已經完整地實現了try...catch...finally語句,但細心的同學會發(fā)現,這個實現有一點小問題。通常的try...catch...finally語句,try部分有且只有1個,finally部分是可選的,但最多只能有1個,而catch部分也是可選的,可以有0到n個,也就是catch部分可以有任意多個。但前面的實現,Catch函數只能指定一個,如果要指定任意多個應該如何做呢?其實很簡單,用一個Catch函數集合保存所有指定的Catch函數即可。不過需要快速定位某一個Catch函數。在Java中,是通過異常類型(如IOException、Exception等)定位特定的catch子句的,我們也可以模擬這一過程,通過特定的異常來定位與該異常對應的Catch函數,為了方便,可以用int類型的異常代碼。那么在調用Catch函數之前,就需要通過異常代碼先定位到某一個Catch函數,然后再調用。下面就是完整的實現代碼。

          ?

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          45
          46
          47
          48
          49
          50
          51
          52
          53
          54
          55
          56
          57
          58
          59
          60
          61
          62
          63
          64
          65
          66
          67
          package?main
          ?
          import?(
          ????"log"
          )
          ?
          type?Exception?struct?{
          ????Id int???????// exception id
          ????Msg string???// exception msg
          }
          ?
          type?TryStruct?struct?{
          ????catches?map[int]ExceptionHandler
          ????try???func()
          }
          ?
          func?Try(tryHandler?func()) *TryStruct {
          ????tryStruct := TryStruct{
          ????????catches: make(map[int]ExceptionHandler),
          ????????try: tryHandler,
          ????}
          ????return?&tryStruct
          }
          ?
          ?
          type?ExceptionHandler?func(Exception)
          ?
          func?(this *TryStruct) Catch(exceptionId int, catch?func(Exception)) *TryStruct {
          ????this.catches[exceptionId] = catch
          ????return?this
          }
          ?
          func?(this *TryStruct) Finally(finally?func()) {
          ????defer?func() {
          ????????if?e := recover(); nil != e {
          ?
          ????????????exception := e.(Exception)
          ?
          ????????????if?catch, ok := this.catches[exception.Id]; ok {???????????
          ????????????????catch(exception)
          ????????????}
          ?????????????
          ????????????finally()
          ????????}
          ????}()
          ?????
          ????this.try()
          }
          ?
          func?Throw(id int, msg string) Exception {
          ????panic(Exception{id,msg})
          }
          ?
          func?main() {
          ?
          ????exception.Try(func() {
          ????????log.Println("try...")
          ???????????????//? 指定了異常代碼為2,錯誤信息為error2
          ????????exception.Throw(2,"error2")
          ????}).Catch(1,?func(e exception.Exception) {
          ????????log.Println(e.Id,e.Msg)
          ????}).Catch(2,?func(e exception.Exception) {
          ????????log.Println(e.Id,e.Msg)
          ????}).Finally(func() {
          ????????log.Println("finally")
          ????})
          }

            執(zhí)行結果如下圖所示。

          ?

          2.png

          ?

          這個實現與Java中的try...catch...finally的唯一區(qū)別就是必須要調用Finally函數,因為處理異常的代碼都在Finally函數中。不過這并不影響使用,如果finally部分沒什么需要處理的,那么就設置一個空函數即可。

          為了方便大家,我已經將該實現封裝成了函數庫,調用代碼如下:

          ?

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          package?main
          import?(
          ????"exception"
          ????"log"
          )
          ?
          func?main() {
          ?
          ????exception.Try(func() {
          ????????log.Println("try...")
          ????????exception.Throw(2,"error2")
          ????}).Catch(1,?func(e exception.Exception) {
          ????????log.Println(e.Id,e.Msg)
          ????}).Catch(2,?func(e exception.Exception) {
          ????????log.Println(e.Id,e.Msg)
          ????}).Finally(func() {
          ????????log.Println("finally")
          ????})
          }


           

          作者:李寧

          原文鏈接:https://www.cnblogs.com/nokiaguy/p/10892183.html

          本文轉自博客園網,版權歸原作者所有。

          瀏覽 62
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产特级黄色片 | 老熟女朝逼| 中文字幕无码AV | 国产精品二区高清在线苍井空 | 亚洲AV毛片在线观看。。 |