PHP Exception Handling class Badmethodcallexception use method

Source: Internet
Author: User
Tags exception handling getmessage php exception handling strlen yii


Badmethodcallexception is the exception handling class in the PHP standard library, it is PHP, such as in many frameworks can see inherited badmethodcallexception class, such as Yii2:


namespace Yii\base;

/**
* Invalidcallexception represents a exception caused by calling a to wrong.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
Class Invalidcallexception extends \badmethodcallexception
{
/**
* @return String The user-friendly name of this exception
*/
Public Function GetName ()
{
Return to ' Invalid call ';
}
}

The Badmethodcallexception class is also inherited from the badfunctioncallexception:


Badmethodcallexception extends Badfunctioncallexception {
/* Inherited Properties * *
protected string $message;
protected int $code;
protected string $file;
protected int $line;
/* Inheritance Method * *
Final public string exception::getmessage (void)
Final public Exception exception::getprevious (void)
Final public int exception::getcode (void)
Final public string exception::getfile (void)
Final public int exception::getline (void)
Final public array exception::gettrace (void)
Final public string exception::gettraceasstring (void)
public string exception::__tostring (void)
Final private void exception::__clone (void)
}

Usage, in control:


Public Function Actionresetpassword ($token)
{
try {
$model = new Resetpasswordform ($token);
catch (Invalidparamexception $e) {
throw new Badrequesthttpexception ($e->getmessage ());
}

if ($model->load (Yii:: $app->request->post ()) && $model->validate () && $model-> ResetPassword ()) {
Yii:: $app->getsession ()->setflash (' Success ', ' New password was saved. ');

return $this->gohome ();
}

return $this->render (' ResetPassword ', [
' Model ' => $model,
]);
}

Used to catch exceptions.

Of course, in addition to the above mentioned also exception handling class exception


1, first PHP5 provides the basic exception handling class, can use directly

<?php
Class Exception
{
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; Name of the exception that occurred
protected $line; Line number of the code where the exception occurred
function __construct ($message = null, $code = 0);
Final function getMessage (); Return exception information
Final function GetCode (); Return exception code
Final function getFile (); Returns the file name where the exception occurred
Final function getline (); Returns the line number of the code where the exception occurred
Final function gettrace (); BackTrace () array
Final function gettraceasstring (); Gettrace () information that has been formatted as a string
/* Can be overloaded method * *
function __tostring (); A string that can be exported
}
?>

The simple use is as follows: (through exception, throw error message)

try {
$error = ' my error! ';
throw new Exception ($error)
catch (Exception $e) {
echo $e->getmessage ();
}

2, we can extend this class to facilitate our use
 
class MyException extends Exception
{
//redefine the constructor so that the message becomes a property that must be specified
The Public function __construct ($message, $code = 0) {
//Custom code
//ensures that all variables are assigned the correct value
Parent::__construct ($message, $code);
}
//Custom string output style
Public Function __tostring () {
return __class__. ": [{$this->code}]: {$this->message}\n";
}
Public Function customfunction () {
echo ' A Custom function for this type of exception\n ';
}
}

The basic idea of the

exception handling is that the code is invoked to execute in the try code. If an error occurs in the try block, we can perform a handle that throws an exception. Some programming languages, such as Java, will automatically throw exceptions in certain cases. In PHP, exceptions must be thrown manually. You can throw an exception by using the following method:
Throw new Exception (' message ', code); The
Throw keyword triggers the exception handling mechanism, which is a language structure, not a function, but it must be passed a value. It requires an accepted object. In the simplest case, you can instantiate a built-in exception class.
Finally, after the try code, you must give at least one catch code block. Multiple catch code blocks can be associated with a try block of code. If each catch code block can catch a different type of exception, it makes sense to use multiple catch blocks of code. For example, if you want to catch the exception of the Exception class, the code
 
catch (Exception $e)
{
//handing Exception
}
Catch code captures the object that is The object that caused the exception and passed to the throw statement (thrown by the throw statement). It is a good choice to use an instance of the exception class. The
Exception class provides the following built-in methods:
GetCode ()-Returns the code passed to the constructor.
GetMessage ()-Returns the message passed to the constructor.
GetFile ()-Returns the path of the file that generated the exception code
Getline ()-Returns the line that contains the code that generated the exception.

Attention:
When an exception is caught, subsequent code inside the try () block will not continue, but will attempt to find a matching "catch" code block
When an exception is thrown, the "uncaught exception ' exception" error is reported if no catch processing is performed

<?php
function test ($val) {
if ($val >100) {
throw new Exception ("Hint: The value you entered is too large");
}
}
Test (111);
?>

3. When an exception is thrown, catch statement blocks can be processed or not processed
Here are some of the code for my user registration feature

try{
Check forms filled in
if (!filled_out ($_post)) {
throw new Exception (' You haven't filled out the form, please go back to fill in ');
}
Check email Address not valid
if (!check_email ($email)) {
throw new Exception (' Incorrect format of message ');
}
Check that the density is longer than 6
if (strlen ($passwd <6)) {
throw new Exception (' The length of the density should be greater than 6 ');
}
Check to see if the password is equal two times
if ($passwd!= $passwd 1) {
throw new Exception (' two times password is different, please re-enter ');
}
Check that the user name is the correct length
if (strlen ($username) >16) {
throw new Exception (' username length does not match, please re-enter ');
}
catch (Exception $e) {
echo $e->getmessage (); Output exception information.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.