Aubergine
Aubergine

Reputation: 6042

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();

Upvotes: 0

Views: 545

Answers (4)

whitehat
whitehat

Reputation: 2391

ClassNotFoundException or SQLException are checked exceptions. So are supposed to be handled somehow whenever they are thrown (even if manually using 'throw' keyword).

There are two ways to handle a checked exception : 1. Surround the code that could throw a checked exception within try-catch block. 2. Add throws clause after the method definition in which that particular code is written.

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.

However, UnsupportedAddressTypeException is an unchecked exception. So you need not handle it explicitly. You can just throw it anywhere and Java will take care of it.

Upvotes: 0

dMb
dMb

Reputation: 9367

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.

The code you included rethrows Exception, which is a checked exception. Unless the method this was incide was declared with "throws Exception" this could couldn't have worked in any Java version.

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

Upvotes: 0

Manish
Manish

Reputation: 3968

eclipse says its an error because:

The method which contains this block of code needs to have a declaration of "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).

Your uni code worked because the method which had these statements must have been declared like:

method x() throws ClassNotFoundException, SQLException

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503429

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.

Upvotes: 6

Related Questions