1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > StrToIntDef > try, catchは不要?

Last updated at Posted at 2017-10-23
動作環境
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をする意味はない、という認識を持った。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?