動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)
StrToIntDef()をtry, catchするかどうか。
Unit1.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include "Unit1.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int val;
try {
val = StrToInt(L"");
} catch (Exception &exc) {
String msg = L"Line24: " + exc.Message;
OutputDebugString(msg.c_str());
}
try {
val = StrToIntDef(L"", -1);
} catch (Exception &exc) {
String msg = L"Line31: " + exc.Message;
OutputDebugString(msg.c_str());
}
}
//---------------------------------------------------------------------------
run
デバッグ出力: Line24: '' は整数ではありません プロセス Project1.exe (3984)
StrToInt 関数は,10 進または 16 進表記の整数型の数値を表す文字列 S を数値に変換します。S が有効な数値を表していない場合,StrToInt は EConvertError 例外を生成します。
StrToIntDef 関数は,10 進または 16 進表記の整数型の数値を表す文字列 S を数値に変換します。S が有効な数値を表していない場合,StrToIntDef は Default を返します。
StrToIntDef()に関しては、try, catchをする意味はない、という認識を持った。