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();
Aubergine
  • 5,862
  • 19
  • 66
  • 110

4 Answers4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @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 :-) – Stephen C Nov 17 '11 at 06:48
  • I appreciate that! :-) Time for coffee break. – Aubergine Nov 17 '11 at 07:03
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

Manish
  • 3,913
  • 2
  • 29
  • 45
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);
    }
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
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.

whitehat
  • 2,381
  • 8
  • 34
  • 47