Elm - 錯誤處理



錯誤是程式中任何意外情況。錯誤可能發生在編譯時或執行時。編譯時錯誤發生在程式編譯期間(例如,程式語法錯誤),而執行時錯誤發生在程式執行期間。與其他程式語言不同,Elm 不會丟擲執行時錯誤。

考慮一個接受使用者年齡的應用程式。如果年齡為零或負數,應用程式應該丟擲錯誤。在這種情況下,Elm 應用程式可以使用錯誤處理的概念在執行時顯式地引發錯誤,如果使用者輸入零或負值作為年齡。錯誤處理指定如果在程式執行期間發生任何意外情況,則採取的行動方案。

Elm 程式語言透過以下方式處理錯誤:

  • MayBe
  • Result

MayBe

考慮應用程式中的搜尋功能。如果找到搜尋關鍵字,則搜尋函式返回相關資料,否則不返回任何內容。此用例可以使用 MayBe 型別在 Elm 中實現。

語法

variable_name:MayBe data_type

MayBe 型別的變數可以包含以下值之一:

  • Just some_Value - 如果存在有效資料,則使用此值。

  • Nothing - 如果值不存在或未知,則使用此值。Nothing 等效於其他程式語言中的 null。

示例

以下示例顯示瞭如何在變數和函式中使用 MayBe 型別。

步驟 1 - 建立一個 MayBeDemo.elm 檔案,並將以下程式碼新增到其中

-- MayBeDemo.elm
module MayBeDemo exposing(..)
import Maybe

--declaring a MayBe variable and assigning value to it
userName : Maybe String
userName = Just "Mohtashim"

--declaring a MayBe variable and assigning value to it
userAge :Maybe Int
userAge = Just 20

--declaring a MayBe variable and assigning value to it
userSalary:Maybe Float
userSalary = Nothing

--declaring a custom type
type Country = India | China | SriLanka

--defining a function that takes a String parameter as input and returns a value of type MayBe

getCountryFromString : String -> Maybe Country
getCountryFromString p =
case p of
   "India"
      -> Just India
   "China"
      -> Just China
   "SriLanka"
      -> Just SriLanka
   _
      -> Nothing

步驟 2 - 在 elm repl 中匯入模組並按如下所示執行

E:\ElmWorks\ErroApp> elm repl
---- elm-repl 0.18.0 -----------------------------------------------------------
:help for help, :exit to exit, more at 
--------------------------------------------------------------------------------
> import MayBeDemo exposing(..)
> userName
Just "Mohtashim" : Maybe.Maybe String
> userAge
Just 20 : Maybe.Maybe Int
> userSalary
Nothing : Maybe.Maybe Float
> getCountryFromString "India"
Just India : Maybe.Maybe MayBeDemo.Country
> getCountryFromString "india"
Nothing : Maybe.Maybe MayBeDemo.Country

該函式檢查傳遞給函式的值是否為印度、中國或斯里蘭卡。如果引數的值與這些值都不匹配,則返回 nothing。

Result

考慮一個示例,其中應用程式需要驗證某些條件,如果不滿足條件則引發錯誤。可以使用 Result 型別來實現這一點。如果應用程式希望顯式地引發錯誤並返回有關錯誤原因的詳細資訊,則應使用 Result 型別。

語法

Result 型別宣告採用兩個引數 - 錯誤的資料型別(通常為 String)以及如果一切正常則要返回的結果的資料型別。

type Result error_type data_value_type
= Ok data_value
| Err error_message

Result 型別返回以下值之一:

  • Ok some_value - 表示要返回的結果

  • Err - 表示如果未滿足預期條件則要返回的錯誤訊息。

示例 1

在 Elm REPL 中嘗試以下示例:

> String.toInt
<function> : String -> Result.Result String Int
-- successful result
> String.toInt "10"
Ok 10 : Result.Result String Int
-- unsuccessful result , Error
> String.toInt "a"
Err "could not convert string 'a' to an Int" : Result.Result String Int

String.toInt 函式如果傳遞的引數有效則返回 Integer 值。如果引數不是數字,則函式返回錯誤。

示例 2

以下示例接受年齡作為引數。如果年齡在 0 到 135 之間,則函式返回年齡,否則返回相應的錯誤訊息。

步驟 1 - 建立一個 ResultDemo.elm 檔案,並將以下程式碼新增到其中。

--ResultDemo.elm
module ResultDemo exposing(..)

userId : Result String Int
userId = Ok 10

emailId : Result String Int
emailId = Err "Not valid emailId"

isReasonableAge : String -> Result String Int
isReasonableAge input =
   case String.toInt input of
      Err r ->
         Err "That is not a age!"

   Ok age ->
      if age < 0 then
         Err "Please try again ,age can't be negative"
      else if age > 135 then
         Err "Please try agian,age can't be this big.."

   else
      Ok age

步驟 2 - 在 elm 包中匯入模組並按如下所示執行

E:\ElmWorks\ElmRepo\15_ErrorHandling\15_Code> elm repl
---- elm-repl 0.18.0 -----------------------------------------------------------
:help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> import ResultDemo exposing (..)
> userId
Ok 10 : Result.Result String Int
> emailId
Err "Not valid emailId" : Result.Result String Int
> isReasonableAge "10"
Ok 10 : Result.Result String Int
> isReasonableAge "abc"
Err "That is not a age!" : Result.Result String Int
廣告

© . All rights reserved.