Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
5.5.0 - 日本語

Change language to:
English - Français - Português - Русский

Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
See the recommended documentation of this function

Scilabヘルプ >> Scilab > Scilab keywords > try

try

try-catch 制御命令のtryブロックを開始

catch

try-catch 制御命令のcatchブロックを開始

呼出し手順

try
statements
catch
statements
end

説明

try-catch 制御構文 はエラーを発生する可能性があるコードを管理するために 使用されます.

try-catch制御命令が 実行されている場合,通常, trycatchキーワードの 間の命令のみが実行されます. しかし,これらの命令のどれかでエラーが発生した場合, catchキーワードまでの命令はスキップされ, catch および end キーワードの間の命令が,デフォルトのエラーモードで 実行されます (errcatch参照).

lasterror 関数により 記録されたエラーを取得することができます.

代替命令が指定されない場合,catch 命令 は無視されます.

エラー処理用の'errcatch'引数を指定して execstr 関数を 使用することも可能であることに注意してください. これは,特に構文エラーを処理する際に有用です.

try-catch は に多少なりとも似ていることにも注意してください:

if execstr("<try instructions>","errcatch")<>0 then
  <catch instructions>
end

この命令は,errcatchと同様の 内部機構を使用しています. これが,errcatch または execstr(...,"errcatch")try-catch 制御構造の中に含めることができない理由です. このコンテキストは検出され, 特定のエラーメッセージが発生します (この誤差はtryでトリガーされた場合の 他の誤差と同様に捕捉,保持されます).

しかし, try-catch 制御構造はネスト可能です (以下の例2を参照ください).

// 例 1
file_path=TMPDIR+'/wrong'
try
  u=mopen(file_path,'r')
  x=mget(10,'c',u)
catch
  disp(['file '+file_path+ 'cannot be read',
        'using default values for x'])
  x=1:10
end 
 [error_message,error_number]=lasterror(%t)

// 例 2 (ネストしたtry/catch構造)
function nestedtry(a, b)
disp("START")
mprintf("\ta is %s\t\tb is %s\n",string(a),string(b))
try
  disp("try 1")
  try
    disp("try 2")
    z=a+1;  // err when string
  catch
    disp("catch 2")
    t=b+1;  // err when string
  end
  disp("after try 2")
catch
  disp("catch 1")
end
disp("after try 1 - THE END")
endfunction
nestedtry(1,1)
nestedtry("a string",1)
nestedtry(1,"a string")
nestedtry("a string","a string")

参照

  • error — エラーメッセージ
  • execstr — 文字列中のScilabコードを実行
  • if — 条件付き実行
  • lasterror — 直近に記録されたエラーメッセージを取得
  • errcatch — エラーの捕捉
Report an issue
<< tilde Scilab keywords Control flow >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Fri Apr 11 14:18:51 CEST 2014