Atlas-Launcher/external/angelscript/docs/manual/doc_cpp_exceptions.html

162 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: C++ exceptions and longjmp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('doc_cpp_exceptions.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">C++ exceptions and longjmp </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="doc_cpp_exceptions_1"></a>
Exceptions</h1>
<p>Application functions and class methods registered with the script engine are allowed to throw C++ exceptions. The virtual machine will automatically catch any C++ exception, abort the script execution, and return control to the application.</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classas_i_script_context.html">asIScriptContext</a> *ctx = engine-&gt;CreateContext();</div>
<div class="line">ctx-&gt;<a class="code hl_function" href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a">Prepare</a>(engine-&gt;GetModule(<span class="stringliteral">&quot;test&quot;</span>)-&gt;GetFunctionByName(<span class="stringliteral">&quot;func&quot;</span>));</div>
<div class="line"><span class="keywordtype">int</span> r = ctx-&gt;<a class="code hl_function" href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84">Execute</a>();</div>
<div class="line"><span class="keywordflow">if</span>( r == <a class="code hl_enumvalue" href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69aa3d548fa7d2278d848e50222b700c6c8">asEXECUTION_EXCEPTION</a> )</div>
<div class="line">{</div>
<div class="line"> <span class="keywordtype">string</span> err = ctx-&gt;<a class="code hl_function" href="classas_i_script_context.html#a0f0efea3955879074cfffa71991f780a">GetExceptionString</a>();</div>
<div class="line"> <span class="keywordflow">if</span>( err == <span class="stringliteral">&quot;Caught an exception from the application&quot;</span> )</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// An application function threw an exception while being invoked from the script</span></div>
<div class="line"> ...</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="ttc" id="aangelscript_8h_html_a867f14b4137dd4602fda1e616b217a69aa3d548fa7d2278d848e50222b700c6c8"><div class="ttname"><a href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69aa3d548fa7d2278d848e50222b700c6c8">asEXECUTION_EXCEPTION</a></div><div class="ttdeci">@ asEXECUTION_EXCEPTION</div><div class="ttdoc">The execution was terminated by an unhandled script exception.</div><div class="ttdef"><b>Definition:</b> angelscript.h:408</div></div>
<div class="ttc" id="aclassas_i_script_context_html"><div class="ttname"><a href="classas_i_script_context.html">asIScriptContext</a></div><div class="ttdoc">The interface to the virtual machine.</div><div class="ttdef"><b>Definition:</b> angelscript.h:2735</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a0f0efea3955879074cfffa71991f780a"><div class="ttname"><a href="classas_i_script_context.html#a0f0efea3955879074cfffa71991f780a">asIScriptContext::GetExceptionString</a></div><div class="ttdeci">virtual const char * GetExceptionString()=0</div><div class="ttdoc">Returns the exception string text.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a43976f42dfc6c1af23e132d36265173a"><div class="ttname"><a href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a">asIScriptContext::Prepare</a></div><div class="ttdeci">virtual int Prepare(asIScriptFunction *func)=0</div><div class="ttdoc">Prepares the context for execution of the function.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a8e52894432737acac2e1a422e496bf84"><div class="ttname"><a href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84">asIScriptContext::Execute</a></div><div class="ttdeci">virtual int Execute()=0</div><div class="ttdoc">Executes the prepared function.</div></div>
</div><!-- fragment --><p>By default the VM has no way of distinguishing between different types of exceptions and will just give a standard exception string for all of them. If desired a <a class="el" href="classas_i_script_engine.html#addce6a202934a4918f7776b2f77c43fd">callback can be registered</a> with the engine to provide a translation of the exception type to a more informative exception string.</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> TranslateException(<a class="code hl_class" href="classas_i_script_context.html">asIScriptContext</a> *ctx, <span class="keywordtype">void</span>* <span class="comment">/*userParam*/</span>)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">try</span> </div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Retrow the original exception so we can catch it again</span></div>
<div class="line"> <span class="keywordflow">throw</span>;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">catch</span>( std::exception &amp;e )</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Tell the VM the type of exception that occurred</span></div>
<div class="line"> ctx-&gt;<a class="code hl_function" href="classas_i_script_context.html#a6f0f071215e0f7effc0b765043f01275">SetException</a>(e.what());</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">catch</span>(...)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// The callback must not allow any exception to be thrown, but it is not necessary </span></div>
<div class="line"> <span class="comment">// to explicitly set an exception string if the default exception string is sufficient</span></div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Register the callback with the engine</span></div>
<div class="line">engine-&gt;SetTranslateAppExceptionCallback(<a class="code hl_define" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae">asFUNCTION</a>(TranslateException), 0, <a class="code hl_enumvalue" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4">asCALL_CDECL</a>);</div>
<div class="ttc" id="aangelscript_8h_html_a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4"><div class="ttname"><a href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4">asCALL_CDECL</a></div><div class="ttdeci">@ asCALL_CDECL</div><div class="ttdoc">A cdecl function.</div><div class="ttdef"><b>Definition:</b> angelscript.h:230</div></div>
<div class="ttc" id="aangelscript_8h_html_a78f8f2c7f1c88b12e74a5ac47b4184ae"><div class="ttname"><a href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae">asFUNCTION</a></div><div class="ttdeci">#define asFUNCTION(f)</div><div class="ttdoc">Returns an asSFuncPtr representing the function specified by the name.</div><div class="ttdef"><b>Definition:</b> angelscript.h:685</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a6f0f071215e0f7effc0b765043f01275"><div class="ttname"><a href="classas_i_script_context.html#a6f0f071215e0f7effc0b765043f01275">asIScriptContext::SetException</a></div><div class="ttdeci">virtual int SetException(const char *info, bool allowCatch=true)=0</div><div class="ttdoc">Sets an exception, which aborts the execution.</div></div>
</div><!-- fragment --><dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_addon_helpers.html">GetExceptionInfo</a> helper function</dd></dl>
<dl class="section note"><dt>Note</dt><dd>The ability to catch exceptions can be turned off by compiling the library with the AS_NO_EXCEPTIONS defined. If this is done, the application should not register any functions that may throw exceptions, as the end result will be undefined should an exception occur.</dd></dl>
<h1><a class="anchor" id="doc_cpp_exceptions_2"></a>
longjmp</h1>
<p>Some applications uses longjmp to do error handling. When performing a longjmp to a previously saved state, there is no chance for the code to do a cleanup of all that happened after the saved state. Because of that the application must not register functions that can perform a longjmp out from the function, as that can leave the virtual machine in an undefined state. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sun May 21 2023 10:12:40 for AngelScript by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6 </li>
</ul>
</div>
</body>
</html>