81 lines
9.3 KiB
HTML
81 lines
9.3 KiB
HTML
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Error handling</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch24.html" title="Chapter 24. Linear Barcodes (One Dimensional Barcodes)"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Error handling</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 24. Linear Barcodes (One Dimensional Barcodes)</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Error handling"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2596419"></a>Error handling</h2></div></div></div>
|
||
|
||
<p>The barcode module uses the standard library error handling which mean that in
|
||
case of an error (most likely that the data validation fails) an exception will be
|
||
raised. Errors can be handled in two ways.</p>
|
||
<p>
|
||
</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
|
||
<p>By enclosing the script in a <code class="code">try { ... } catch { ... }</code>
|
||
statement</p>
|
||
</li><li class="listitem">
|
||
<p>By installing a custom default error handler with
|
||
<code class="code">set_exception_handler()</code> when the library throws an
|
||
error the specified error handler will be called with an instance of the
|
||
<code class="code">JpGraphException</code> class.</p>
|
||
</li></ol></div><p>
|
||
</p>
|
||
<p>The following code snippet shows an example of using a try-catch statment</p>
|
||
<p>
|
||
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
||
</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-identifier">BarcodeFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_CODE39</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$e</span><span class="hl-code"> = </span><span class="hl-identifier">BackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">,</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$e</span><span class="hl-code">-></span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">abc123</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">JpGraphException</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">Error: </span><span class="hl-quotes">'</span><span class="hl-code"> . </span><span class="hl-var">$e</span><span class="hl-code">-></span><span class="hl-identifier">getMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div><p>
|
||
</p>
|
||
<p>The code when run will give the error</p>
|
||
<p>
|
||
</p><pre class="screen">Error: Data validation failed. Can't encode [abc123] using encoding "CODE 39" </pre><p>
|
||
</p>
|
||
<p>The problem with the input data string is that Code 39 does not support encoding
|
||
lower case letters. In the case some error handling is still needed but the image
|
||
error should be displayed it is possible to re-raise the original exception as the
|
||
following example shows</p>
|
||
<p>
|
||
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
||
</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-identifier">BarcodeFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_CODE39</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$e</span><span class="hl-code"> = </span><span class="hl-identifier">BackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">,</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$e</span><span class="hl-code">-></span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">abc123</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">JpGraphException</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">Raise</span><span class="hl-brackets">(</span><span class="hl-var">$e</span><span class="hl-code">-></span><span class="hl-identifier">getMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div><p>
|
||
</p>
|
||
<p>The line</p>
|
||
<p>
|
||
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">JpGraphError::Raise($e->getMessage());</span></pre></td></tr></table></div><p>
|
||
</p>
|
||
<p>will display the image error as shown in <a class="xref" href="ch24s06.html#fig.barcode-validation-error" title="Figure 24.5. Image error - Failed barcode data validation">Figure 24.5. Image error - Failed barcode data validation</a></p>
|
||
<p>
|
||
</p><div class="figure"><a name="fig.barcode-validation-error"></a><p class="title"><b>Figure 24.5. Image error - Failed barcode data validation</b></p><div class="figure-contents">
|
||
|
||
<div class="mediaobject"><img src="images/barcode_data_validation_failed.png" alt="Image error - Failed barcode data validation"></div>
|
||
</div></div><p><br class="figure-break">
|
||
</p>
|
||
<p>In the command line barcode utility ( <a class="xref" href="ch24s07.html" title="Generating barcodes on the command line">Generating barcodes on the command line</a>) the alternative method of installing a different default error handler can be
|
||
seen.</p>
|
||
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch24.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>
|