【www.gdgbn.com--淘宝规则】

目中把错误等级设置为:error_reporting(e_all | e_strict);

数组变量未定义也会报错,其实挺好的,但有时候真的不需要报该错误,php教程的解决办法是:

@$_get["unkown"];

这样就可以放置该错误提示出来了.

exception:

throw new exception("username already taken");

 

更甚的情况,如果你认为客户端并不想用过多的操作而仅仅想看到异常信息,你可以抛出一个unchecked exception:

throw new runtimeexception("username already taken");

 

另外,你可以提供一个方法来验证该username是否被占用。

 

很有必要再重申一下,checked exception应该让客户端从中得到丰富的信息。要想让你的代码更加易读,请倾向于用unchecked excetpion来处理程序中的错误(prefer unchecked exceptions for all programmatic errors)。


4. document exceptions.

你可以通过javadoc’s @throws 标签来说明(document)你的api中要抛出checked exception或者unchecked exception。然而,我更倾向于使用来单元测试来说明(document)异常。不管你采用哪中方式,你要让客户端代码知道你的api中所要抛出的异常。这里有一个用单元测试来测试indexoutofboundsexception的例子:

public void testindexoutofboundsexception() {
    arraylist blanklist = new arraylist();
    try {
        blanklist.get(10);
        fail("should raise an indexoutofboundsexception");
    } catch (indexoutofboundsexception success) {}
}


上边的代码在请求blanklist.get(10)的时候会抛出indexoutofboundsexception,如果没有被抛出,将 fail("should raise an indexoutofboundsexception")显示说明该测试失败。通过书写测试异常的单元测试,你不但可以看到异常是怎样的工作的,而且你可以让你的代码变得越来越健壮。

 

下面作者将介绍界中使用异常的最佳实践(best practices for using exceptions)
1.  总是要做一些清理工作(always clean up after yourself)

如果你使用一些资源例如数据库教程连接或者网络连接,请记住要做一些清理工作(如关闭数据库连接或者网络连接),如果你的api抛出unchecked exception,那么你要用try-finally来做必要的清理工作:

 

public void dataaccesscode(){
    connection conn = null;
    try{
        conn = getconnection();
        ..some code that throws sqlexception
    }catch(sqlexception ex){
        ex.printstacktrace();
    } finally{
        dbutil.closeconnection(conn);
    }
}

class dbutil{
    public static void closeconnection
        (connection conn){
        try{
            conn.close();
        } catch(sqlexception ex){
            logger.error("cannot close connection");
            throw new runtimeexception(ex);
        }
    }
}


dbutil是一个工具类来关闭connection.有必要的说的使用的finally的重要性是不管程序是否碰到异常,它都会被执行。在上边的例子中,finally中关闭连接,如果在关闭连接的时候出现错误就抛出runtimeexception.


2. 不要使用异常来控制流程(never use exceptions for flow control)

下边代码中,maximumcountreachedexception被用于控制流程:

public void useexceptionsforflowcontrol() {
    try {
        while (true) {
            increasecount();
        }
    } catch (maximumcountreachedexception ex) {
    }
    //continue execution
}

public void increasecount()
    throws maximumcountreachedexception {
    if (count >= 5000)
        throw new maximumcountreachedexception();
}


上边的useexceptionsforflowcontrol()用一个无限循环来增加count直到抛出异常,这种做法并没有说让代码不易读,但是它是程序执行效率降低。

记住,只在要会抛出异常的地方进行异常处理。

 

3. 不要忽略异常

当有异常被抛出的时候,如果你不想恢复它,那么你要毫不犹豫的将其转换为unchecked exception,而不是用一个空的catch块或者什么也不做来忽略它,以至于从表面来看象是什么也没有发生一样。


  4. 不要捕获顶层的exception

unchecked exception都是runtimeexception的子类,runtimeexception又继承exception,因此,如果单纯的捕获exception,那么你同样也捕获了runtimeexception,如下代码:

try{
..
}catch(exception ex){
}

一旦你写出了上边的代码(注意catch块是空的),它将忽略所有的异常,包括unchecked exception.


5. log exceptions just once

    logging the same exception stack trace more than once can confuse the programmer examining the stack trace about the original source of exception. so just log it once.

 

本文来源:http://www.gdgbn.com/taobaodaxue/28733/