0

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

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Aubergine - it was always like that. The requirement that a method declares checked exceptions that it throws / propagates has been in Java since 1.0, and probably earlier. You might be going crazy too :-)
I appreciate that! :-) Time for coffee break.
0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.