国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Visual Basic .NET中的異常處理簡介(下)

2024-07-10 13:02:24
字體:
供稿:網(wǎng)友
visual basic .net中的異常處理簡介(下)

作者:microsoft  
  非結(jié)構(gòu)化異常處理
  非結(jié)構(gòu)化異常處理通過 err 對象和以下三種語句來實現(xiàn):on error、resume 和 error。on error 語句創(chuàng)建單個異常處理程序以捕捉發(fā)生的所有異常,您可以在以后改變處理程序的位置,但一次只能有一個處理程序。此方法可以跟蹤最近產(chǎn)生的異常和最近的異常處理程序的位置。在方法開始時,異常和異常處理程序的位置都設(shè)置為 nothing。
  要在代碼中生成運行時錯誤,請使用 raise 方法。每次在錯誤處理例程中發(fā)生 exit sub、exit function、exit property、resume 或 resume next 語句時,err 對象的屬性都將重置為零或零長度字符串。在錯誤處理例程外部使用上述任何語句都不會重置其屬性。如果確實需要重置屬性,可以使用 clear 方法重置 err 對象。  error 對象
  err 對象屬性的值由剛剛發(fā)生的錯誤決定。下表列出了該對象的屬性及其簡單說明。 屬性 說明
description 對錯誤進行簡單說明的文本消息。
helpcontext 整數(shù),包含幫助文件中某個主題的上下文標(biāo)識符。
helpfile 字符串表達(dá)式,包含幫助文件的完全限定路徑。
lastdll 由于調(diào)用動態(tài)鏈接庫 (dll) 而產(chǎn)生的系統(tǒng)錯誤代碼。此 dll 是發(fā)生錯誤之前最后調(diào)用的 dll。
number 指定錯誤的數(shù)值。
source 字符串表達(dá)式,代表產(chǎn)生錯誤的對象或應(yīng)用程序。

  下面的示例顯示了如何在非結(jié)構(gòu)化錯誤處理中使用上述某些屬性: on error resume next
err.clear
err.raise(33333)
err.description = "您沒有輸入數(shù)字!"
msgbox(err.number)
msgbox(err.description)
msg = "請按 f1 或“幫助”查看 " & err.helpfile & " 中有關(guān)以下幫助內(nèi)容的" & _
"主題:" & err.helpcontext
msgbox(msg)

  on error goto 語句
  on error goto 語句啟用異常處理的某個例程,并指定該例程在此過程中的位置。它使用標(biāo)簽或行號,指出特定異常處理例程在代碼中的位置。使用 -1 時,在過程內(nèi)部禁用錯誤處理。使用 0 時,禁用當(dāng)前異常。如果沒有 on error 語句,并且在當(dāng)前調(diào)用堆棧中所有方法均未處理異常,則發(fā)生任何運行時錯誤都將是致命的:執(zhí)行過程停止并顯示錯誤消息。
  下表列出了 on error goto 語句可能使用的方法。 語句 任務(wù)
on error goto -1 將 err 對象重置為 nothing,從而在例程中禁用錯誤處理
on error goto 0 將最后的異常處理程序位置重置為 nothing,從而禁用異常
on error goto <標(biāo)簽名> 將指定標(biāo)簽設(shè)置為異常處理程序的位置
on error resume next 創(chuàng)建 resume next 行為,作為最近的異常處理程序的位置

  resume 和 resume next
  resume 語句本身可以將控制權(quán)返回導(dǎo)致異常的語句。執(zhí)行過程將返回到最初產(chǎn)生異常的那一行。
  相比較而言,resume next 語句將在發(fā)生異常后恢復(fù)執(zhí)行過程。該語句指定,在異常事件中,控制權(quán)將傳遞給緊接發(fā)生異常語句之后的語句。resume next 的使用可允許出現(xiàn)不太嚴(yán)重的失敗。引發(fā)錯誤的語句失敗,但應(yīng)用程序?qū)⒗^續(xù)執(zhí)行,且允許用戶改正錯誤并繼續(xù)進行操作。與此類似,resume <標(biāo)簽> 將控制權(quán)傳遞給在其 line 參數(shù)中指定的標(biāo)簽。確保行標(biāo)簽與調(diào)用它的代碼位于相同的過程中,因為它不能跨函數(shù)使用。
  resume 在錯誤處理例程中必須單獨使用。它在這種例程的外部引發(fā)錯誤。  error 語句
  visual basic .net 支持 error 語句僅僅是為了保持向后兼容。在新代碼中,使用 err 對象的 raise 方法生成運行時錯誤。  非結(jié)構(gòu)化異常處理示例
  以下示例是非結(jié)構(gòu)化錯誤處理的一種基本方法。當(dāng) flawlesscode 遇到錯誤時,執(zhí)行過程將轉(zhuǎn)移到 whoops,它為用戶提供該錯誤的有關(guān)信息(主要包含在 err 對象的 description 屬性中的信息): private sub flawlesscode()
on error goto whoops
  ' 代碼要做很多事情,不要過多
  ' 地研究錯誤處理代碼。
  return
whoops:
' 為用戶提供錯誤信息。
  msgbox ("意外錯誤:" & err.description)
  return
end sub

  以下示例顯示了如何使用 err 對象構(gòu)造錯誤消息對話框。 dim errormessage as string
' 如果發(fā)生錯誤則構(gòu)造錯誤消息。
on error resume next
err.raise (13) ' 生成“類型不匹配”錯誤。
' 查看是否出現(xiàn)錯誤。如果是,則顯示消息。
if err.number <> 0 then
  errormessage = "錯誤 #" & str(err.number) & " 原因是" _
   & err.source & vbcrlf & err.description
' 將該消息顯示為關(guān)鍵消息。
  msgbox(errormessage, msgboxstyle.critical, "錯誤")
end if
  總結(jié)
  到現(xiàn)在為止,您應(yīng)該清楚地了解了結(jié)構(gòu)化異常處理和非結(jié)構(gòu)化異常處理之間的區(qū)別,以及 visual basic .net 中結(jié)構(gòu)化異常處理功能的優(yōu)勢。通常情況下,結(jié)構(gòu)化異常處理即可滿足您的需求,但在少數(shù)情況下仍然可能需要使用非結(jié)構(gòu)化異常處理。
  在確保異常得到處理的同時,不要過多地列舉它們,否則會導(dǎo)致性能下降。try 結(jié)構(gòu)是很有條理的,易于編寫且易于閱讀,它能生成有效的代碼。編寫處理一個或多個可能異常的代碼時,都應(yīng)該使用該結(jié)構(gòu)。此方法極其有效,您甚至愿意在正常情況下使用異常來控制邏輯流程。例如,替代 if 或 select 語句。處理異常是很有效的,但應(yīng)該到真正發(fā)生異常時使用。
  下表列出了預(yù)定義的異常類及其起因和派生類。
表 1 異常類 產(chǎn)生原因 派生類
appdomainunloadedexception 嘗試訪問未加載的應(yīng)用程序域 無
argumentexception 為方法提供的一個或多個參數(shù)無效 argumentnullexception
argumentoutofrangeexception
componentmodel.invalidenum
argumentexception
duplicatewaitobjectexception
arithmeticexception 在算法、強制類型轉(zhuǎn)換或轉(zhuǎn)換操作上發(fā)生錯誤 dividebyzeroexception
notfinitenumberexception
overflowexception
arraytypemismatchexception 嘗試在數(shù)組中存儲錯誤類型的元素 無
badimageformatexception dll 或可執(zhí)行程序的文件映像無效 無
cannotunloadappdomainexception 嘗試卸載應(yīng)用程序域失敗 無
componentmodel.design.serialization.
codedomserializerexception 產(chǎn)生序列化錯誤的行號信息 無
componentmodel.licenseexception 無法為組件授予許可證 無
componentmodel.warningexception 異常被作為警告而不是錯誤處理 無
configuration.configurationexception 配置設(shè)置中發(fā)生錯誤 無
configuration.install.installexception 在安裝過程的提交、回滾或卸載階段發(fā)生錯誤 無
contextmarshalexception 嘗試通過上下文范圍封送對象失敗 無
data.dataexception 使用 ado.net 組件時產(chǎn)生錯誤 data.constraintexception
data.deletedrowinaccessibleexception
data.duplicatenameexception
data.inrowchangingeventexception
data.invalidconstraintexception
data.invalidexpressionexception
data.missingprimarykeyexception
data.nonullalllowedexception
data.readonlyexception
data.rownotintableexception
data.stringtypingexception
data.typeddatasetgeneratorexception
data.versionnotfoundexception
data.dbconcurrencyexception 在升級操作中,dataadapter 確定受影響的行數(shù)等于零 無
data.sqlclient.sqlexception sql server 返回警告或錯誤 無
data.sqltypes.sqltypeexception data.sqltypes 的異常基類 data.sqltypes.sqlnullvalueexception
data.sqltypes.sqltruncateexception
drawing.printing.
invalidprinterexception 使用無效的打印機設(shè)置嘗試訪問打印機 無
enterpriseservices.
registrationexception 檢測到注冊錯誤 無
enterpriseservices.serviced
componentexception 在運行的組件上檢測到錯誤 無
executionengineexception 在公共語言運行時的執(zhí)行引擎上存在內(nèi)部錯誤 無
formatexception 參數(shù)的格式不符合調(diào)用方法的參數(shù)規(guī)定 net.cookieexception
reflection.customattribute
formatexception
uriformatexception
indexoutofrangeexception 嘗試訪問其索引在數(shù)組范圍之外的數(shù)組元素 無
invalidcastexception 無效的強制類型轉(zhuǎn)換或顯式轉(zhuǎn)換 無
invalidoperationexception 方法調(diào)用對于對象的當(dāng)前狀態(tài)無效 net.protocolviolationexception
net.webexception
objectdisposedexception
invalidprogramexception 程序包含無效的 microsoft 中間語言或元數(shù)據(jù) 無
io.internalbufferoverflowexception 內(nèi)部緩沖區(qū)溢出 無
io.ioexception 發(fā)生 i/o 錯誤 io.directorynotfoundexception
io.endofstreamexception
io.fileloadexception
io.filenotfoundexception
io.pathtoolongexception
management.managementexception 管理錯誤 無
memberaccessexception 嘗試訪問類成員失敗 fieldaccessexception
methodaccessexception
missingfieldexception
missingmemberexception
missingmethodexception
multicastnotsupportedexception 嘗試組合兩個無法組合的代理類型實例,兩者的操作數(shù)都為非空引用 無
notimplementedexception 未執(zhí)行要求的方法或操作 無
notsupportedexception 不支持所調(diào)用的方法,或者嘗試在不支持所調(diào)用函數(shù)的流中進行讀取、查找或?qū)懭?platformnotsupportedexception
nullreferenceexception 嘗試取消引用空對象引用 無
outofmemoryexception 內(nèi)存不足以完成執(zhí)行程序 無
rankexception 將具有錯誤維數(shù)的數(shù)組傳遞給方法 無
reflection.ambiguousmatch
exception 綁定方法時導(dǎo)致多個方法符合綁定條件 無
reflection.reflectiontype
loadexception module.gettypes 方法導(dǎo)致模塊中的一個或多個類無法加載 無
resources.missingmanifest
resourceexception 主要程序集不包含非特定語言的資源,但它們又是必需的,因為缺少合適的輔助程序集 無
runtime.interopservices.
externalexception 所有 com 互操作異常和結(jié)構(gòu)化異常處理異常的基本異常類型 componentmodel.design.
checkoutexception
componentmodel.win32exception
data.oledb.oledbexception
messaging.messagequeueexception
runtime.interopservices.comexception
runtime.interopservices.sehexception
web.httpexception
runtime.interopservices.
invalidcomobjectexception 使用了無效的 com 對象 無
runtime.interopservices.
invalidolevarianttypeexception 封送器遇到無法封送到管理代碼的變體類型參數(shù) 無
runtime.interopservices.
marshaldirectiveexception 封送器遇到不支持的 marshalasattribute 無
runtime.interopservices.
safearrayrankmismatchexception 傳入 safearray 的名次與管理簽名中指定的名次不匹配 無
runtime.interopservices.
safearraytypemismatchexception 傳入 safearray 的類型與管理簽名中指定的類型不匹配 無
runtime.remoting.remotingexception 遠(yuǎn)程操作時發(fā)生錯誤 runtime.remoting.remoting
timeoutexception
runtime.remoting.serverexception 用于在客戶端連接到無法產(chǎn)生異常的非 .net 框架應(yīng)用程序時傳遞異常 無
runtime.serialization.
serializationexception 序列化或反序列化過程中發(fā)生錯誤 無
security.crytography.
cryptographicexception 加密操作過程中發(fā)生錯誤 security.cryptography.
cryptographicunexpected
operationexception
security.policy.policyexception 策略禁止代碼運行 無
security.securityexception 檢測到安全性錯誤 無
security.verificationexception 安全策略要求代碼的類型安全,而驗證程序無法驗證代碼是否類型安全 無
security.xmlsyntaxexception xml 分析時出現(xiàn)語法錯誤 無
serviceprocess.timeoutexception 指定的超時已過期 無
stackoverflowexception 待定的方法調(diào)用太多,導(dǎo)致執(zhí)行堆棧溢出 無
threading.synchronizationlockexception 在代碼的異步塊中調(diào)用同步方法 無
threading.threadabortexception 調(diào)用 abort 方法 無
threading.threadinterruptedexception 在 waitsleepjoin 狀態(tài)時線程中斷 無
threading.threadstateexception 方法調(diào)用的無效 threadstate 中的線程 無
typeinitializationexception 圍繞類初始化程序產(chǎn)生的異常而產(chǎn)生的包裝 無
typeloadexception 類型加載失敗 dllnotfoundexception
entrypointnotfoundexception
typeunloadedexception 嘗試訪問已卸載的類 無
unauthorizedaccessexception 操作系統(tǒng)拒絕訪問,因為存在 i/o 錯誤或特定類型的安全性錯誤 無
web.services.protocols.soapexception 在 soap 上調(diào)用 xml web 服務(wù)方法而導(dǎo)致錯誤 web.services.protocols.
soapheaderexception
xml.schema.xmlschemaexception    無
xml.xmlexception    無
xml.xpath.xpathexception 處理 xpath 表達(dá)式時發(fā)生錯誤 無
xml.xsl.xsltexception 處理可擴展樣式表語言 (xsl) 轉(zhuǎn)換時發(fā)生錯誤 system.xml.xsl.xsltcompileexception
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙江县| 工布江达县| 新建县| 水富县| 宣恩县| 恩施市| 马关县| 吐鲁番市| 丰顺县| 武强县| 冕宁县| 噶尔县| 农安县| 萝北县| 哈巴河县| 饶河县| 车致| 遂川县| 连云港市| 钟祥市| 板桥市| 客服| 溧阳市| 砀山县| 北海市| 镇沅| 莱芜市| 林芝县| 阿坝县| 油尖旺区| 伊春市| 辽阳市| 阿坝| SHOW| 始兴县| 义乌市| 申扎县| 天镇县| 淮阳县| 钟山县| 贵港市|