java抛出检查异常

java抛出检查异常

本文介绍了java抛出检查异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这以前曾经奏效,但是日食说投掷线有错误.

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;
    }

那太完美了.

仅此类型有效,但这不是我想要的

Only this type works, but it is not what I want

 throw new UnsupportedAddressTypeException();

推荐答案

大概是声明了您的方法以抛出 UnsupportedAddressTypeException 而不是 SQLException ClassNotFoundException .只能从声明已抛出异常或超类的方法中抛出已检查异常(包括重新抛出现有异常).

Presumably your method is declared to throw UnsupportedAddressTypeException but not SQLException and ClassNotFoundException. Checked exceptions can only be thrown (including rethrowing an existing one) from methods which declare that they throw those exceptions or a superclass.

这篇关于java抛出检查异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:48