W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
try/catch/rescue/after
塊中的變量定義不會(huì)泄露到外部內(nèi)容中.這是因?yàn)?code style="font-size: 15px;">try塊可能會(huì)失敗,因此這些變量可能永遠(yuǎn)不會(huì)被綁定在第一位.換句話說,這個(gè)代碼是非法的:ex> try do
...> raise "fail"
...> what_happened = :did_not_raise
...> rescue
...> _ -> what_happened = :rescued
...> end
iex> what_happened
** (RuntimeError) undefined function: what_happened/0
作為替代,你可以存儲(chǔ)?try
?表達(dá)式的值:
iex> what_happened =
...> try do
...> raise "fail"
...> :did_not_raise
...> rescue
...> _ -> :rescued
...> end
iex> what_happened
:rescued
對try
,catch
和rescue
的介紹到此結(jié)束.你會(huì)發(fā)現(xiàn)相較于其他語言,在Elixir中較少用到它們,盡管在某些庫或特定的代碼沒有"按照規(guī)則"書寫時(shí),它們很有用.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: