異常處理基本語法

2021-10-07 02:42:30 字數 1107 閱讀 5566

異常發生第一現場,丟擲異常

void function( )

在需要關注異常的地方,捕捉異常

trycatch(異常型別宣告)catch(異常型別 形參)catch(…)

注意事項:

通過throw操作建立乙個異常物件並拋擲

在需要捕捉異常的地方,將可能丟擲異常的程式段嵌在try塊之中

按正常的程式順序執行到達try語句,然後執行try塊{}內的保護段

如果在保護段執行期間沒有引起異常,那麼跟在try塊後的catch子句就不執行,程式從try塊後跟隨的最後乙個catch子句後面的語句繼續執行下去

catch子句按其在try塊後出現的順序被檢查,匹配的catch子句將捕獲並按catch子句中的**處理異常(或繼續拋擲異常)

 如果沒有找到匹配,則預設功能是呼叫abort終止程式。

// p15-3.2 異常處理基本語法.cpp : 此檔案包含 「main」 函式。程式執行將在此處開始並結束。

//#include

#include

#include

#include

using namespace std;

#define bufsize 1024

int copyfile2(const char* dest,const char* src)

fopen_s(&fp2, dest, "wb");

if (fp2 == null)

char buffer[bufsiz];

int readlen, writelen;

while ((readlen=fread(buffer,1,bufsiz,fp1))>0)

}fclose(fp1);

fclose(fp2);

return 0;int copyfile1(const char* dest, const char* src)

catch (float e)

return 0;

}int main()

catch (int error)

catch (string* error)

catch (float error)

catch (...)

C 異常處理(一) 基本語法

define crt secure no warnings include include using namespace std void testerror int x,int y cout 計算結果 void main catch int x catch system pause void t...

python異常處理語法

目錄 raise 丟擲異常 使用者自定義異常 try語句總結 斷言異常 源 mts desktop mts document python cat try.py import os try raise oserror os.mkdir new 人為丟擲異常 except oserror as e p...

C 異常基本語法

define crt secure no warnings includeusing namespace std class myexception class person person int mydivide int a int b return a b void test01 catch i...