{"id":1236,"date":"2022-08-30T15:14:39","date_gmt":"2022-08-30T15:14:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/unknown-exception-error-in-php-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:39","modified_gmt":"2022-08-30T15:14:39","slug":"unknown-exception-error-in-php-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/unknown-exception-error-in-php-collection-of-common-programming-errors\/","title":{"rendered":"unknown exception error in php-Collection of common programming errors"},"content":{"rendered":"<p>i wanna catch all exceptions thrown in a script and then check if they have a error code 23000.<\/p>\n<p>if they don&#8217;t i want to rethrow the exception.<\/p>\n<p>here is my code:<\/p>\n<pre><code>function myException($exception) {\n    \/*** If it is a Doctrine Connection Mysql Duplication Exception ***\/\n    if(get_class($exception) === 'Doctrine_Connection_Mysql_Exception' &amp;&amp; $exception-&gt;getCode() === 23000) {\n         echo \"Duplicate entry\";\n    } else {\n         throw $exception;\n    }\n}\n\nset_exception_handler('myException');\n\n$contact = new Contact();\n$contact-&gt;email = 'peter';\n$contact-&gt;save();\n<\/code><\/pre>\n<p>but i get this error message and i dont know what it means:<\/p>\n<pre><code>Fatal error: Exception thrown without a stack frame in Unknown on line 0\n<\/code><\/pre>\n<p>i want to be able to rethrow the original error message if it has not the error code 23000.<\/p>\n<p>even when i deleted the check errorcode i still get the same message:<\/p>\n<pre><code>function myException($exception) {\n    throw $exception;\n}\n\nset_exception_handler('myException');\n\n$contact = new Contact();\n$contact-&gt;email = 'peter';\n$contact-&gt;save();\n<\/code><\/pre>\n<p>how could i solve this?<\/p>\n<p>thanks<\/p>\n<ol>\n<li>\n<p>Don&#8217;t use the exception handler for this.<\/p>\n<p>The exception handler is only the &#8220;last resort&#8221; to handle <em>uncaught<\/em> exceptions.<\/p>\n<p>You can&#8217;t throw a new exception within it &#8211; it would lead to an infinite loop.<\/p>\n<p>The regular way to handle exceptions is using a <code>try... catch {}<\/code> block:<\/p>\n<pre><code>try\n {\n  $contact-&gt;save();\n }\ncatch (Exception $exception)\n {\n  if(get_class($exception) === 'Doctrine_Connection_Mysql_Exception' \n     &amp;&amp; $exception-&gt;getCode() === 23000) {\n         echo \"Duplicate entry\";\n    } else {\n         throw $exception; \/\/ throw it on if it's not a doctrine exception\n                           \/\/ (if that's what you want)\n    }\n\n }\n<\/code><\/pre>\n<p>I know this looks way more messy than the way you do it. That&#8217;s why I&#8217;m not really fond of exceptions.<\/p>\n<\/li>\n<li>\n<p>i&#8217;m not sure but try this code<\/p>\n<pre><code>function myException($exception) {\n    restore_exception_handler();\n    throw $exception;\n}\n\/\/you can set here another exception handler that will be restored.\n\/\/or your exception will be thrown to standard handler\n\/\/set_exception_handler('myException2');\n\nset_exception_handler('myException');\n\n$contact = new Contact();\n$contact-&gt;email = 'peter';\n$contact-&gt;save();\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:25. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i wanna catch all exceptions thrown in a script and then check if they have a error code 23000. if they don&#8217;t i want to rethrow the exception. here is my code: function myException($exception) { \/*** If it is a Doctrine Connection Mysql Duplication Exception ***\/ if(get_class($exception) === &#8216;Doctrine_Connection_Mysql_Exception&#8217; &amp;&amp; $exception-&gt;getCode() === 23000) { echo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1236","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1236"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1236\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}