0

Suppose I have the following lines of code,

       Class.forName(JDBC_DRIVER);
       dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);

Now what I want is that when an SQLException is caught, I want to throw my custom exception from the catch field of the SQLException, I mean is it possible to do so or is there an alternative way to do so?

AND my custom exception is ErrorToDisplayException as:

    public class ErrorToDisplayException extends Exception{

public ErrorToDisplayException(Throwable e) {
}
    }

my code is as:

   try {        
    //Register JDBC driver
       Class.forName(JDBC_DRIVER);
       dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);
   }catch(final SQLException se){
    // Handle errors for JDBC
       throw new ErrorToDisplayException(se);
   }

Now what happens is that when the compiler reaches at }catch(final SQLException se){ it does not go to its catch body and just breaks away, don't know why?

Amir
  • 685
  • 3
  • 13
  • 36
  • Yes I have already done so, but what happens is that when an SQLException occurs (during DEBUG mode), it catches the exception and straightly breaks away from the code rather than to execute its catch field!, don't know why its happening! – Amir Mar 12 '14 at 10:08
  • It is an odd behavior. Could you edit your original post with a snippet of the code where you have observed this? Maybe it is some unchecked exception what happens to slip out of your catch() – Jorge_B Mar 12 '14 at 10:12
  • Could you post also the value of JDBC_DRIVER? Are you sure this class is reachable in your classpath? – Jorge_B Mar 12 '14 at 10:24
  • @Jorge_B JDBC_DRIVER = "com.mysql.jdbc.Driver" ,yes I am sure there no problem of classpath! – Amir Mar 12 '14 at 10:26
  • Then my last idea would be to add something like `catch(Throwable t) { t.printStackTrace(); }` at the end of your code in order to exhaust all the possible options of a checked exception being thrown – Jorge_B Mar 12 '14 at 10:29
  • @Jorge_B , its working when I entered an incorrect username! but why its not working when the database is not live while username or password is correct? – Amir Mar 12 '14 at 10:35
  • Maybe the driver throws some unchecked exception in case of a communication error, but a checked `SQLException` when it is live but you type a wrong password... It is just guessing, but in that case the driver implementation would not be behaving very kindly to its contract :P – Jorge_B Mar 12 '14 at 10:41

2 Answers2

1

You mean somthing like that or I don't get it?!

try {
    Class.forName(JDBC_DRIVER);
    dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (SQLException e) {
    throw new MyException(e);
}

or you mean to replace some standard exception by own type in all places?

Anver
  • 116
  • 2
  • Yes I did the same way at the very start, BUT what problem I am facing is that when the compiler goes to line, "} catch (SQLException e) {", then it does not go to its field and just breaks away, don't know why! – Amir Mar 12 '14 at 10:15
  • Then maybe you don't have any exception in catch block at all. Are you sure that SQLException is really thrown in the block of code mentioned above? – Anver Mar 12 '14 at 10:18
  • please see my edited code now, hope you get an idea now! – Amir Mar 12 '14 at 10:21
  • Amir, can you try to set INCORRECT user / pass for your database and rerun your code? Will it help to get catch block be executed? – Anver Mar 12 '14 at 10:28
  • yea a bundle of thanks, it worked after inserting the incorrect username. BUT what I want is that it should work when username/password is correct but database is down or not live! – Amir Mar 12 '14 at 10:32
  • Well, in that case please answer this question - are you sure that your database is really unavailable at the moment of code execution? Check your ports, maybe some DB listener is alive. Because right now it looks that DB is available and your app can be connected to that. – Anver Mar 12 '14 at 10:36
  • Yes I am 100% sure that the database is not available! (I terminated mySQL as well!) – Amir Mar 12 '14 at 10:38
  • Unbelievable :) Do you have own custom ports configuration for MySQL? And what type of MySQL installation do you have (in your local machine or somewhere in the network)? – Anver Mar 12 '14 at 10:40
  • Yes its on my local machine! – Amir Mar 12 '14 at 10:41
  • If you working on Windows please check if you have 'LISTENING' state for 3306 port by executing following command in cmd: netstat -an – Anver Mar 12 '14 at 10:47
  • Yes I verified it, its LISTENING – Amir Mar 12 '14 at 10:49
  • 3306 is default port of MySQL listener. That means your DB is online and ready to recieve requests. That's why you don't have any exceptions while code execution. – Anver Mar 12 '14 at 10:51
  • I found a way...what I am doing now is that I wrote, finally { throw new ErrorToDisplayException(); } and its working! :P – Amir Mar 12 '14 at 10:52
  • Emmm... Amir, 'finally' is not what you need. If I understand you right you must thrown your own type of exception ONLY in case of some problems with DB connection. Code inside 'finally' block will be executed always. It is not depends if you have some connection problems or not. – Anver Mar 12 '14 at 10:54
  • Amir, please look here http://stackoverflow.com/questions/10885038/stop-mysql-service-windows Maybe this 'how-to-stop' instructions will help you. – Anver Mar 12 '14 at 10:56
  • do you know how to shutdown database of mysql from cmd using windows? – Amir Mar 12 '14 at 11:07
  • I don't know. From link that I've provided it can be looked like this -> Print to Windows Command line following command: net stop MySQL – Anver Mar 12 '14 at 11:42
0

First:

class MyCustomException extends Exception {
    public MyCustomException(Throwable e) {
    }
}

Then:

try {
    Class.forName(JDBC_DRIVER);
    dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (SQLException e) {
    throw new MyCustomException(e);
}
DmitryKanunnikoff
  • 2,226
  • 2
  • 22
  • 35
  • Yes I did exactly in this way but the compiler does not go in to the catch field of SQLException! – Amir Mar 12 '14 at 10:14