- Exception::getCode
- Examples
- See Also
- User Contributed Notes 4 notes
- php — Get exception name from set_exception_handler function
- Solution:
- Share solution ↓
- Additional Information:
- Didn’t find the answer?
- Similar questions
- Write quick answer
- About the technologies asked in this question
- PHP
- Welcome to programmierfrage.com
- Get answers to specific questions
- Help Others Solve Their Issues
- Latest questions:
- 650 How Can I populate a div if there any change in database using jquery and ajax request in PHP?
- 190 php — WP multisite restore_current_blog() Not Working
- 169 javascript — Fancybox didn’t work while getting images from database (php)
- 40 php — apostrophe is not stored in Mysql database while scraping data from website
- 240 php — How to get user info when user is authenticated with Google Oauth2 and people.me
- 802 Php include, index.php and CSS file problems
- 936 php — Send email from other domain without being spam
- 69 php — Regex match first 4 characters of 2 variables
- 610 php — How to select Distinct on inner join?
- 479 mysql — PHP Sessions and Cookies
- Users questions:
- mysql
- Warning: Undefined array key «program» in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key «enter_course» in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key «enter_address» in E:\xampp\htdocs\pi\alumni.php on line 32
- Rename existing images with keyword in WordPress PHP
- codeigniter
- Class «web_profiler.controller.profiler» does not exist after upgrading symfony 3.4 to 4.4 version
- Exception
- Class synopsis
- Properties
- Table of Contents
- User Contributed Notes 3 notes
Exception::getCode
Returns the exception code as int in Exception but possibly as other type in Exception descendants (for example as string in PDOException ).
Examples
Example #1 Exception::getCode() example
try throw new Exception ( «Some error message» , 30 );
> catch( Exception $e ) echo «The exception code is: » . $e -> getCode ();
>
?>?php
The above example will output something similar to:
See Also
User Contributed Notes 4 notes
The exception code can be used to categorize your errors. If you’re wondering what the exception code can be used for, read on below.
Let’s say each time your application isn’t able to connect to the database, you can save the error message under the error/exception code 214. At the end of the month, you can do a quick search on the error number ‘214’ and find out how many times this error occurred. This makes life easier. Also, the error/exception message will give you details into what happened.
The point is to use both the exception message and code. It’s helpful in the long run.
Note: I added this note, because I was confused earlier as to the purpose of the exception code and it’s use.
when raising an Exception with no error code explicitly defined, getCode() returns the integer 0
try <
throw new Exception ( «no code!!» );
> catch ( Exception $e ) <
print( «Code='» . $e -> getCode () . «‘» );
>
?>
outputs
Do not use the strict operator as suggested when checking the Exception Code in \PDOException.
As per documentation: \PDOException is returning a string for its Exception Code and not an Integer.
Ran into the following in PHP8:
catch(\ PDOException $e ) var_dump ( $e -> getCode ()); //Output: string(5) «23000»
>
One of the most useful tricks of getCode is handling duplication entries when trying to add a duplicate row into the database.
Sometimes we don’t need to show any errors regarding this duplication.
The following snippet is taken from one of my projects:
//I have a mysql table called hosts with one unique key named host_name.
try $hosts = new Hosts ([ «host_name» => «www.example.com» );
$hosts -> save ();
> catch (\ PDOException $e ) if ( $e -> getCode () === 23000 ) //Do nothing
>
>
?>
Here I’m saving the host name, and I don’t care if it’s already exists.
php — Get exception name from set_exception_handler function
Solution:
$exceptionClass = get_class($msg);
Share solution ↓
Additional Information:
Didn’t find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.
Latest questions:
650 How Can I populate a div if there any change in database using jquery and ajax request in PHP?
190 php — WP multisite restore_current_blog() Not Working
169 javascript — Fancybox didn’t work while getting images from database (php)
40 php — apostrophe is not stored in Mysql database while scraping data from website
240 php — How to get user info when user is authenticated with Google Oauth2 and people.me
802 Php include, index.php and CSS file problems
936 php — Send email from other domain without being spam
69 php — Regex match first 4 characters of 2 variables
610 php — How to select Distinct on inner join?
479 mysql — PHP Sessions and Cookies
Users questions:
mysql
Warning: Undefined array key «program» in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key «enter_course» in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key «enter_address» in E:\xampp\htdocs\pi\alumni.php on line 32
Rename existing images with keyword in WordPress PHP
codeigniter
Class «web_profiler.controller.profiler» does not exist after upgrading symfony 3.4 to 4.4 version
© 2021-2023 Programming problem solving site for beginners and advanced. Answers to questions related to coding.
This site uses cookies. We use them to improve the performance of our website and your interaction with it. Confirm your consent by clicking OK
Exception
Exception is the base class for all user exceptions.
Class synopsis
Properties
The filename where the exception was created
The line where the exception was created
The previously thrown exception
The string representation of the stack trace
The stack trace as an array
Table of Contents
- Exception::__construct — Construct the exception
- Exception::getMessage — Gets the Exception message
- Exception::getPrevious — Returns previous Throwable
- Exception::getCode — Gets the Exception code
- Exception::getFile — Gets the file in which the exception was created
- Exception::getLine — Gets the line in which the exception was created
- Exception::getTrace — Gets the stack trace
- Exception::getTraceAsString — Gets the stack trace as a string
- Exception::__toString — String representation of the exception
- Exception::__clone — Clone the exception
User Contributed Notes 3 notes
Lists of Throwable and Exception tree as of 7.2.0
Error
ArithmeticError
DivisionByZeroError
AssertionError
ParseError
TypeError
ArgumentCountError
Exception
ClosedGeneratorException
DOMException
ErrorException
IntlException
LogicException
BadFunctionCallException
BadMethodCallException
DomainException
InvalidArgumentException
LengthException
OutOfRangeException
PharException
ReflectionException
RuntimeException
OutOfBoundsException
OverflowException
PDOException
RangeException
UnderflowException
UnexpectedValueException
SodiumException
Note that an exception’s properties are populated when the exception is *created*, not when it is thrown. Throwing the exception does not seem to modify them.
Among other things, this means:
* The exception will blame the line that created it, not the line that threw it.
* Unlike in some other languages, rethrowing an exception doesn’t muck up the trace.
* A thrown exception and an unthrown one look basically identical. On my machine, the only visible difference is that a thrown exception has an `xdebug_message` property while an unthrown one doesn’t. Of course, if you don’t have xdebug installed, you won’t even get that.
Note: this documentation not full, ReflectionObject::export($exception):
Object of class [ class Exception implements Throwable ] — Properties [ 7 ] Property [ protected $message ]
Property [ private $string ]
Property [ protected $code ]
Property [ protected $file ]
Property [ protected $line ]
Property [ private $trace ]
Property [ private $previous ]
>
— Methods [ 11 ] Method [ final private method __clone ] >
Method [ public method __construct ]
— Parameters [ 3 ] Parameter #0 [ $message ]
Parameter #1 [ $code ]
Parameter #2 [ $previous ]
>
>
Method [ public method __wakeup ] >
Method [ final public method getMessage ] >
Method [ final public method getCode ] >
Method [ final public method getFile ] >
Method [ final public method getLine ] >
Method [ final public method getTrace ] >
Method [ final public method getPrevious ] >
Method [ final public method getTraceAsString ] >
Method [ public method __toString ] >
>
>
?>
Missed:
Property [ private $string ]
Property [ private $trace ]
Property [ private $previous ]
Method [ public method __wakeup ] >
- Predefined Exceptions
- Exception
- ErrorException
- Error
- ArgumentCountError
- ArithmeticError
- AssertionError
- DivisionByZeroError
- CompileError
- ParseError
- TypeError
- ValueError
- UnhandledMatchError
- FiberError