PHP variable definition and exception handling (1/3)

Source: Internet
Author: User
Tags exception handling php tutorial

The error level is set to: error_reporting (E_all | e_strict);

The array variable is not defined and will also report an error, but sometimes it really doesn't need to be reported, the PHP tutorial solution is:

@$_get[' Unkown '];

This will allow you to place the error prompt.

exception

throw new Exception ("Username already taken");

Even more, if you think that the client does not want to use too much action and just want to see the exception information, you can throw a unchecked exception:

throw new RuntimeException ("username already taken");

Alternatively, you can provide a way to verify that the username is occupied.

It is necessary to reiterate that checked exception should let the client get rich information from it. To make your code more readable, be inclined to use unchecked excetpion to handle errors in your program (prefer unchecked exceptions for all programmatic errors).


4. Document exceptions.

You can use the Javadoc ' s @throws tag to illustrate (document) Your API to throw checked exception or unchecked exception. However, I prefer to use unit tests to illustrate (document) exceptions. Whichever way you use it, you have to let the client code know the exception that you want to throw in your API. Here's an example of using unit tests to test indexoutofboundsexception:

public void Testindexoutofboundsexception () {
ArrayList blanklist = new ArrayList ();
try {
Blanklist.get (10);
Fail ("Should raise an indexoutofboundsexception");
catch (Indexoutofboundsexception success) {}
}


The top code throws a indexoutofboundsexception when the request Blanklist.get (10), and if not thrown, fail ("should raise an indexoutofboundsexception ") shows that the test failed. By writing a unit test of a test exception, you can not only see how the exception works, but you can make your code more and more robust.

The following authors will introduce the best practices for using exceptions in the world (top practices for using exceptions)
1. Always have to do some cleanup (always clean up after yourself)

If you use resources such as a database tutorial or a network connection, remember to do some cleanup (such as shutting down a database connection or a network connection), and if your API throws unchecked exception, then you need to use try-finally to do the necessary cleanup work:

Home 1 2 3 last

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.