<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
//<![CDATA[
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
//]]>
</script>
<style type="text/css">/*<![CDATA[*/
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
/*]]>*/</style>
<script type="text/javascript">//<![CDATA[
gRootRelPath = "../../..";
gCommonRootRelPath = "../../..";
gTopicId = "9.1.2.0_13";
//]]></script>
<script type="text/javascript" src="../../../template/scripts/rh.min.js"></script>
<script type="text/javascript" src="../../../template/scripts/common.min.js"></script>
<script type="text/javascript" src="../../../template/scripts/topic.min.js"></script>
<script type="text/javascript" src="../../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../../whxdata/projectsettings.js"></script>
<link rel="stylesheet" type="text/css" href="../../../template/styles/topic.min.css"/>
<link rel="stylesheet" type="text/css" href="../../../template/Charcoal_Grey/topicheader.css"/>
<meta name="topic-status" content="Draft"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>try / catch / finally</title>
<meta name="generator" content="Adobe RoboHelp 2019"/>
<link rel="stylesheet" href="../../../assets/css/default.css" type="text/css"/>
<meta name="rh-authors" content="Mark Alexander"/>
<meta name="topic-comment" content="Page outlining the try / catch / finally functions"/>
<meta name="rh-index-keywords" content="try,catch,finally"/>
<meta name="search-keywords" content="try,catch,finally"/>
</head>
<body>
<div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
<div class="logo">
</div>
<div class="nav">
<div class="title" title="try / catch / finally">
<span>try / catch / finally</span>
</div>
<div class="gotohome" title="Click here to see this page in full context">
<span>Click here to see this page in full context</span>
</div>
</div>
</div>
<div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>
<!--<div class="body-scroll" style="top: 150px;">-->
<h1>try / catch / finally</h1>
<p>The <tt>try</tt>, <tt>catch</tt> and <tt>finally</tt> statements can be used in your game for error checking and permit you to test out blocks of code and control what happens if any <a href="../../../Additional_Information/Errors/Runner_Errors.htm">runtime exceptions</a> occur. Using these will prevent the exception ending the game and showing the standard error message to the user, but this means that you will have to handle what happens next in that case, like saving out log files - for example - and ending the
game gracefully (note that if you choose to do nothing, your game may become unstable and not perform correctly).</p>
<p>At it's most basic the <tt>try</tt> syntax is as follows:</p>
<p class="code">try<br/> {
<br/> <statement1>;
<br/> <statement2>;
<br/> ...<br/> }
</p>
<p>However, having a <tt>try</tt> without anything to actually handle any exceptions the code may produce will not be very helpful, so we usually pair it with a <tt>catch</tt>, with the following syntax:</p>
<p class="code">try<br/> {
<br/> <statement1>;
<br/> <statement2>;
<br/> ...
<br/> }
<br/> catch(<variable>)
<br/> {
<br/> <statement1>;
<br/> <statement2>;
<br/> ...
<br/> }
</p>
<p>What <tt>catch</tt> does is permit you to run extra code supplied in the following block when an exception from the previous <tt>try</tt> has been caught. If this is a runtime exception, then the supplied variable can be used to access a <a href="../Structs.htm">struct</a> which will contain the following information:</p>
<p class="code">{<br/> message : "", // a string that is a short message for this exception<br/> longMessage : "", // a string that is a longer message for this exception<br/> script : "", // a string
that describes where the exception came from<br/> stacktrace : [ "", "" ], // an array of strings that is the stack frame the exception was generated<br/> }
</p>
<p>A simple example of use is shown below:</p>
<p class="code">var a = 0, b = 0, c = 0;<br/> try
<br/> {
<br/> c = a div b;<br/> }
<br/> catch( _exception)<br/> {
<br/> show_debug_message(_exception.message);
<br/> show_debug_message(_exception.longMessage);
<br/> show_debug_message(_exception.script);
<br/> show_debug_message(_exception.stacktrace);
<br/> }
</p>
<p>It may be that you want to run some code regardless of whether an exception was thrown or not, and so for that you can add in a <tt>finally</tt> block. The <span class="inline">finally</span> syntax is:</p>
<p class="code">finally<br/> {
<br/> <statement1>;
<br/> <statement2>;
<br/> etc...
<br/> }
</p>
<p>It is worth noting that you can have any combination of these together, ie:</p>
<ul class="colour">
<li><tt>try</tt> / <tt>finally</tt></li>
<li><tt>try</tt> / <tt>catch</tt></li>
<li><tt>try</tt> / <tt>catch</tt> / <tt>finally</tt></li>
</ul>
<p>Note that within the <tt>finally</tt> block you <em>cannot </em>use <a href="break.htm"><tt>break</tt></a>, <a href="continue.htm"><tt>continue</tt></a>, <a href="exit.htm"><tt>exit</tt></a> or <a href="return.htm"><tt>return</tt></a> statements as
they have no meaning in this context and the compiler will generate an error if they are used.</p>
<p>Finally, you can also nest various <tt>try</tt> / <tt>catch</tt> / <tt>finally</tt> within each other, for example:</p>
<p class="code">var a = 0, b = 0, c = 0;<br/> try
<br/> {
<br/> try
<br/> {
<br/> c = a div b;<br/> }
<br/> finally
<br/> {
<br/> ++a;
<br/> }
<br/> }
<br/> catch(_exception)
<br/> {
<br/> ++a;
<br/> show_debug_message(_exception.message);
<br/> show_debug_message(_exception.longMessage);
<br/> show_debug_message(_exception.script);
<br/> show_debug_message(_exception.stacktrace);
<br/> }
<br/> finally
<br/> {
<br/> show_debug_message("a = " + string(a));<br/> }
</p>
<p>It is worth noting that you can take over the default GML error message and use your own handler code by calling the function <a href="../../GML_Reference/Debugging/exception_unhandled_handler.htm"><tt>exception_unhandled_handler()</tt></a>. This
<a href="../Runtime_Functions.htm">runtime function</a> permits you to supply a custom <a href="../Method_Variables.htm">method</a> to use that will be called whenever any unhandled exceptions occur in your game.</p>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="../Language_Features.htm">Language Features</a></div>
<div style="float:right">Next: <a href="throw.htm">throw</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
<!-- KEYWORDS
try
catch
finally
-->
<!-- TAGS
try
catch
finally
-->
</body></html>