public String ProcessA(String a) throws Throwable {
dosomething
}public String ProcessA(String a) throws Exception {
dosomething()
}public String ProcessA(String a) throws OwnException {
dosomething()
}那位大侠可以解释一下 上面三种对exception定义写法有什么不同 和应该在什么地方使用

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【winbug2008】截止到2008-07-02 21:08:07的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    这三种不是对exception的定义啊只是在方法中声明了此方法会抛出何种异常 例如 
    public String ProcessA(String a) throws Exception {
        //dosomething()

    如果没有声明此方法会抛出异常即
    public String ProcessA(String a) {
        //dosomething()
    }
    那么在ProcessA方法中如果使用到了别的会 抛出Exception子类的checked exception那么就必须用 try catch来处理,否则就必须在方法中声明要  throws Exception 
    对于 ProcessA能处理的异常,那么它可以自己用 try catch来处理,对于处理不了的异常或它不应该知道不应该处理得异常,就要向上抛出,由调用此方法的高层方法来处理。至于是throws Throwable throws Exception 或是 throws OwnException  那要看具体情况来定啊 
    一般情况下自己定义的 exception 都是继承与 RuntimeException的,都是unchecked exception不用 throws
      

  3.   

    我的问题可能没有问清楚
    我知道那是异常抛出声明。
    就是我们在定义我的异常抛出声明的时候,上面三种写法是否会产生不同的结果。特别是Throwable,他和下面两种写法到底有什么区别呢?
    谢谢大家了。
      

  4.   

    刚在网上看到这篇文章,有一点启发大家看看All errors and exceptions extend from Throwable. By catching Throwable, it is possible to handle all unexpected conditions.There are several scenarios where it is good practice to catch Throwable. For example, in a server application, the threads that handle requests should catch Throwable and relay any errors or exceptions to the client. Another scenario is a long-running thread that performs some background activity. Such threads should catch Throwable, log any errors or exceptions, and then continue functioning.It is rarely good practice for a method in a library to catch Throwable. In general, errors and exceptions should not be masked from the caller.This example demonstrates a long-running thread that catches Throwable and logs the exception.    class BgThread extends Thread {
            // Create a logger. For more information on the logging api's,
            // see e385 The Quintessential Logging Program
            Logger logger = Logger.getLogger("com.mycompany.mypackage");
        
            BgThread() {
                // As a daemon thread, this thread won't prevent the application from exiting
                setDaemon(true);
            }
        
            // Set to true to shut down this thread
            boolean stop = false;
        
            public void run() {
                while (!stop) {
                    try {
                        // Perform work here
                    } catch (Throwable t) {
                        // Log the exception and continue
                        logger.log(Level.SEVERE, "Unexception exception", t);
                    }
                }
            }
        }
      

  5.   

    还有一篇 我觉得写的也很有道理。贴在这里给大家参考吧
    Don’t use throws Exception or throws ThrowableUsing throws Throwable and throws Exception subverts the exception checking system. If you do this, callers are forced to catch Throwable or Exception, which may catch both unchecked and checked exceptions. This forces callers to use instanceof to see whether the exception is one that they can deal with.Essentially, this defers the exception-checking mechanism from compile-time to runtime, which of course converts compile-time errors to runtime errors.On a related note, there’s not much point in specifying unchecked exception types in a throws clause, for example throws RuntimeException or throws NullPointerException. This serves no purpose except documentation, and it’s better to use a @throws javadoc comment for this, as you can include more information in the javadoc comment.
      

  6.   

    lz啊,我劝你还是先别问这三种由什么不同了,能整明白这一种就行了我都说了,这个方法中会抛出那种checked exception 你就 throws 那种 exception啊
      

  7.   

    -------------------------------------------------------------
                Quietly through  .....