{"id":1037,"date":"2021-04-11T01:38:55","date_gmt":"2021-04-11T01:38:55","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1037"},"modified":"2021-05-21T13:07:45","modified_gmt":"2021-05-21T13:07:45","slug":"php-throw-exception","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-throw-exception\/","title":{"rendered":"PHP throw Exception"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the Exception class in detail and how to throw a new exception in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-exception-class'>Introduction to the Exception class <a href=\"#introduction-to-the-exception-class\" class=\"anchor\" id=\"introduction-to-the-exception-class\" title=\"Anchor for Introduction to the Exception class\">#<\/a><\/h2>\n\n\n\n<p>When encountering a situation from which you cannot recover, you can throw an exception.<\/p>\n\n\n\n<p>An exception is an instance of the <code>Exception<\/code> class. Like other PHP objects, you use the <code>new<\/code> keyword to create an instance of the <code>Exception<\/code> class. <\/p>\n\n\n\n<p>An <code>Exception<\/code> object has two main properties: a message and a numeric code. The message describes the exception. The numeric code is optional, which specifies the context for a specific exception.<\/p>\n\n\n\n<p>When you create a new exception, you provide the mesage the optional numeric code. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$exception = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">Exception<\/span>(<span class=\"hljs-string\">'Invalid username or password'<\/span>, <span class=\"hljs-number\">100<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Exception<\/code> object has the <code>getMessage()<\/code> and <code>getCode()<\/code> that returns the message and numeric code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$exception = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">Exception<\/span>(<span class=\"hljs-string\">'Invalid username or password'<\/span>, <span class=\"hljs-number\">100<\/span>);\n\n$message =  $exception-&gt;getMessage();\n$code =  $exception-&gt;getCode();<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-throw-statement'>The throw statement <a href=\"#the-throw-statement\" class=\"anchor\" id=\"the-throw-statement\" title=\"Anchor for The throw statement\">#<\/a><\/h2>\n\n\n\n<p>In practice, you rarely assign an exception to a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variable<\/a>. Instead, you raise (or throw) the <code>Exception<\/code> object using the <code>throw<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">Exception<\/span>(<span class=\"hljs-string\">'Invalid username or password'<\/span>, <span class=\"hljs-number\">100<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>throw<\/code> statement accepts an instance of the <code>Exception<\/code> class or the subclass of the <code>Exception<\/code> class. In fact, it accepts any object that implements the <code><a href=\"https:\/\/www.php.net\/manual\/en\/class.throwable.php\" target=\"_blank\" rel=\"noreferrer noopener\">Throwable<\/a><\/code> interface. Note that the <code>Exception<\/code> class also implements the <code>Throwable<\/code> interface.<\/p>\n\n\n\n<p>When PHP encounters a <code>throw<\/code> statement, it immediately halts the code execution. Therefore, the code after the throw statement won&#8217;t execute.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='throwing-an-exception-example'>Throwing an exception example <a href=\"#throwing-an-exception-example\" class=\"anchor\" id=\"throwing-an-exception-example\" title=\"Anchor for Throwing an exception example\">#<\/a><\/h3>\n\n\n\n<p>The following example defines a function called <code>divide()<\/code> that uses the the throw statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">divide<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">if<\/span> (!is_numeric($x) || !is_numeric($y)) {\n        <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> InvalidArgumentException(<span class=\"hljs-string\">'Both arguments must be numbers or numeric strings'<\/span>);\n    }\n\n    <span class=\"hljs-keyword\">if<\/span> ($y == <span class=\"hljs-number\">0<\/span>) {\n        <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">Exception<\/span>(<span class=\"hljs-string\">'Division by zero, y cannot be zero'<\/span>);\n    }\n    <span class=\"hljs-keyword\">return<\/span> $x \/ $y;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How the <code>define()<\/code> function works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, throw the <code>InvalidArgumentException<\/code> exception if <code>$x<\/code> and <code>$y<\/code> are not numbers or numeric strings.<\/li><li>Second, throw the division by zero exception if <code>$y<\/code> is zero.<\/li><li>Third, return the division of <code>$x<\/code> and <code>$y<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-built-in-exception-classes'>PHP built-in exception classes <a href=\"#php-built-in-exception-classes\" class=\"anchor\" id=\"php-built-in-exception-classes\" title=\"Anchor for PHP built-in exception classes\">#<\/a><\/h2>\n\n\n\n<p>The standard PHP library (SPL) provides many subclasses of the <code>Exception<\/code> class. For example, you can use the <code>InvalidArgumentException<\/code> when an argument of a function or method is not valid.<\/p>\n\n\n\n<p>The following shows the exception classes in PHP 8.0:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Exception\n   ClosedGeneratorException\n   DOMException\n   ErrorException\n   IntlException\n   JsonException\n   LogicException\n      BadFunctionCallException\n         BadMethodCallException\n      DomainException\n      InvalidArgumentException\n      LengthException\n      OutOfRangeException\n   PharException\n   ReflectionException\n   RuntimeException\n      OutOfBoundsException\n      OverflowException\n      PDOException\n      RangeException\n      UnderflowException\n      UnexpectedValueException\n   SodiumException<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>An exception is an instance of the <code>Exception<\/code> class that implements the <code>Throwable<\/code> interface.<\/li><li>Use the <code>throw<\/code> statement to raise an exception.  The <code>throw<\/code> statement accepts an exception object that implements the <code>Throwable<\/code> interface.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1037\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-throw-exception\/\"\n\t\t\t\tdata-post-title=\"PHP throw Exception\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1037\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-throw-exception\/\"\n\t\t\t\tdata-post-title=\"PHP throw Exception\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the Exception class in detail and how to throw a new exception in PHP.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":33,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1037","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1037\/revisions"}],"predecessor-version":[{"id":1831,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1037\/revisions\/1831"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}