react異常捕獲處理 邊界處理

2021-09-26 15:07:14 字數 1198 閱讀 6340

最近專案全面轉向react,寫了兩個專案後發現render裡面的異常無法捕獲,就無法監控上報錯誤資訊等,後來想了想這麼強大的react,肯定會有處理方法。

componentdidcatch

(error, errorinfo)

第乙個引數是實際丟擲的錯誤,第二個引數是指錯誤資訊。componentdidcatch生命類似於try/catch 語句,它不能捕獲本省的錯誤的錯誤,而是將錯誤傳給離它最近的異常上。

import react,

from

'react'

;class

errcapture

extends

component;}

logerrortomyservice

(error)

=this

.props

if(abnormallog)

}componentdidcatch

(error, info));

this

.logerrortomyservice

(error, info);}

render()

=this

.props

if(this

.state.haserror)

return

this

.props.children;}}

export

default errcapture;

具體呼叫上面捕獲異常元件的方法,componentdidcatch()方法可以幫我們捕捉到login的錯誤資訊了

import react,

from

'react'

;import errcapture from

'../pages/home'

import login from

'../components/login'

class

index

extends

component

}render()

abnormallog=

>

>

<

/errcapture>

<

/div>)}

}export

default index;

10 6 捕獲處理異常

異常處理 方式一 捕獲處理 捕獲處理的格式 trycatch 捕獲的異常型別 變數名 public class captureexception public static void div int a,int b catch arithmeticexception e system.out.pri...

異常捕獲處理(2)

異常捕獲機制 bme中定義了乙個抽象異常類bmeexception,它有兩個子類businessbmeexception和generalbmeexception。一般情況下我們丟擲異常的時候採用throw new buisinessbmeexception 100010101 e 其中 100010...

python異常捕獲處理

一 異常處理 在程式執行過程中,總會遇到各種各樣的錯誤。程式一旦出錯就停止執行了,此時就需要捕捉異常,通過捕捉到的異常,我們再去做對應的處理 寫乙個函式,實現除法運算 def calc a,b return a b print calc 5,1 呼叫,沒有錯誤,結果是5.0 5.0 print ca...