1. 首页
  2. 易语言 - EasyLanguage
  3. 关于保留字 Try-Catch-Finally (Reserved Words)

关于保留字 Try-Catch-Finally (Reserved Words)

Description:

The three reserved word try, catch, and finally work together to make up a code structure that is used to test for exceptions (errors) and handle them when they occur.

Usage:

try
     //Code to be run
catch ( {exception} )
     //Code to be run if the above generates a run time exception/error
end;

or

try
     //Code to be run
catch ( {exception} )
     //Code to be run if the above generates a run time exception/error
finally
     //Final piece of code to be run regardless of whether the above two were successful or unsuccessful
end;

Example

Example usage for a code snippet that gets a numeric value from a Global Dictionary regardless of it’s stored type…

Using elsystem;
Using elsystem.collections;

Vars:
	GlobalDictionary dict(NULL),
	double vdbl(0),
	int vint(0),
	aggcnt(0),
	gdkey("");

Once Begin
	dict = GlobalDictionary.Create(TRUE, "myGlobDictName");
End;

gdkey = "aggcnt";
Try
	vdbl = dict.Items[gdkey] astype double; //try to get aggcnt as a double value
	aggcnt = vdbl;
	print("Got " + gdkey + " as double");
Catch (Exception ex)
	// If we get here then the storage type is probably not double or doesn't exist at all
	vint = dict.Items[gdkey] astype int; // try to get aggcnt as an int value
	aggcnt = vint;
	Print("Got " + gdkey + " as int");
Finally
	Print("this always gets executed");
End;
更新于 2020年5月25日

这篇文章对您有帮助吗?

相关的话题

留言评论