简体   繁体   English

java抛出检查异常?

[英]java throwing checked exceptions?

I am pretty sure that this worked before, but eclipse says there is error with it in throw line. 我很确定这在以前是可行的,但是日食说它在投掷线中有错误。

 try{}
}catch(Exception e){
    throw e;    }

In my old student project I wrote : 在我以前的学生项目中,我写道:

 try {
        Class.forName("org.postgresql.Driver");
        this.connection = DriverManager.getConnection(
                DataSource.getURL(), DataSource.getUserName(), DataSource.getPassword());
    } catch (ClassNotFoundException e) {
        System.out.println("Could not find driver to connect to database. Please make"
                + "sure the correseponding postgreSQLjdbc library is added.");
        throw e;

    } catch (SQLException e) {
        System.out.println("Username or password is not correct");
        throw e;
    }

and it was perfect. 这是完美的。

Only this type works, but it is not what I want 仅此类型有效,但这不是我想要的

 throw new UnsupportedAddressTypeException();

Presumably your method is declared to throw UnsupportedAddressTypeException but not SQLException and ClassNotFoundException . 假定您的方法声明为引发UnsupportedAddressTypeException但不会引发SQLExceptionClassNotFoundException Checked exceptions can only be thrown (including rethrowing an existing one) from methods which declare that they throw those exceptions or a superclass. 只能从声明已抛出异常或超类的方法中抛出已检查异常(包括重新抛出现有异常)。

eclipse says its an error because: eclipse说它是错误的,因为:

The method which contains this block of code needs to have a declaration of "throws Exception" 包含此代码块的方法需要声明“ throws Exception”

The method either needs to handle the exception itself OR signal to other calling methods that it can throw an exception and they should handle it. 该方法要么需要处理异常本身,要么向其他调用方法发出信号,告知它可以引发异常,并且应该处理该异常。

An alternate way of handling it would be to enclose the "throw e" in another try/catch block(method handling the exception itself). 处理它的另一种方法是将“ throw e”包含在另一个try / catch块中(处理异常本身的方法)。

Your uni code worked because the method which had these statements must have been declared like: 您的uni代码起作用了,因为具有这些语句的方法必须声明为:

method x() throws ClassNotFoundException, SQLException 方法x()抛出ClassNotFoundException,SQLException

Without additional information with specifics on what exactly you are doing, the only generic solution I can give you is to rethrow your exception wrapped in an unchecked RuntimeException. 如果没有其他信息来说明您到底在做什么,我唯一可以给您的一般解决方案是将异常包装在未经检查的RuntimeException中。

The code you included rethrows Exception, which is a checked exception. 您包含的代码将引发Exception,这是一个已检查的异常。 Unless the method this was incide was declared with "throws Exception" this could couldn't have worked in any Java version. 除非使用“ throws Exception”声明包含的方法,否则该方法不能在任何Java版本中使用。

On another note, never log and rethrow. 另一方面,请勿登录并重新抛出。 Do either one or the other. 做一个或另一个。 So your project code should've looked like: 因此,您的项目代码应如下所示:

 ....

     } catch (SQLException e) {
        throw RuntimeException("Username or password is not correct", e);
    }

ClassNotFoundException or SQLException are checked exceptions. ClassNotFoundException或SQLException是检查的异常。 So are supposed to be handled somehow whenever they are thrown (even if manually using 'throw' keyword). 所以应该在抛出它们时以某种方式处理(即使使用'throw'关键字手动)。

There are two ways to handle a checked exception : 1. Surround the code that could throw a checked exception within try-catch block. 有两种方法来处理已检查的异常:1.将可能引发已检查的异常的代码括在try-catch块中。 2. Add throws clause after the method definition in which that particular code is written. 2.在编写特定代码的方法定义之后添加throws子句。

Now, here when you 'throw e' where e is either an instance of ClassNotFoundException or SQLException, it has to be handled in either of the above two ways. 现在,在这里当您“抛出e”(其中e是ClassNotFoundException或SQLException的实例)时,必须以上述两种方式之一对其进行处理。

However, UnsupportedAddressTypeException is an unchecked exception. 但是,UnsupportedAddressTypeException是未经检查的异常。 So you need not handle it explicitly. 因此,您无需显式处理它。 You can just throw it anywhere and Java will take care of it. 您可以将其扔到任何地方,Java会照顾好它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM