I hava a problem running the following code.
Where localhost does not run a mysql server.
import java.sql.*;
public class ExceptionTest
{
public static void main(String[] args)
{
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception e) { }
Connection conn;
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?user=root");
} catch (Exception e) {
System.err.println("Exception : " + e.getClass().getName());
}
}
}
this throws an SQLException (that i catch) and a ConnectException at the
getConnection line.
How ever this code altso throws an SQLException that i catch, but altso a
ConnectException that i do not catch.
import java.sql.*;
public class ExceptionTest
{
public static void main(String[] args)
{
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception e) { }
Connection conn;
try {
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?user=root");
} catch (Exception e) {
System.err.println("Exception : " + e.getClass().getName());
}
} catch (Exception e) {
System.err.println("Exception : " + e.getClass().getName());
}
}
}
Why can't I catch the ConnectException???
-andreas (andr...@stud.ntnu.no)
<snip>
> Why can't I catch the ConnectException???
You're never explicitly catching SQLException - just Exception, which
isn't good style to start with.
Only one exception is *ever* thrown at a time. It's impossible for two
exceptions to be thrown at once. What might happen is that SQLException
might *contain* a ConnectException as the cause of the SQLException.
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
ok, i remoed the try/catch, and added throws SQLExeption to the main method:
import java.sql.*;
public class ExceptionTest
{
public static void main(String[] args) throws SQLException
{
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception e) { }
Connection conn;
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?user=root");
}
}
running it:
$> java ExceptionTest
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.gjt.mm.mysql.MysqlIO.<init>(MysqlIO.java:114)
at org.gjt.mm.mysql.Connection.<init>(Connection.java:229)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ExceptionTest.main(ExceptionTest.java:13)
Exception in thread "main" java.sql.SQLException: Cannot connect to MySQL
server
on localhost:3306. Is there a MySQL server running on the machine/port you
are
trying to connect to? (java.net.ConnectException)
at org.gjt.mm.mysql.Connection.<init>(Connection.java:239)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ExceptionTest.main(ExceptionTest.java:13)
I see two exceptions? why??? is the ConnectException in another thread???
-andreas
That's still not good - you'll have no way of knowing if the driver
itself can't be found.
No - the SQLException is *wrapping* a ConnectionException. Only the
SQLException is being thrown.
I'm not sure i understand, how do i catch the ConnectionException (and the
SQLException)? can you give me some example code?
-andreas
You don't catch the ConnectionException, because it isn't thrown to your
level of code. You catch SQLException with
catch (SQLException e)
{
}
As it happens, I've checked and for some reason SQLException doesn't
give you the actual exception thrown underneath - but if there are more
exceptions in the chain, you can use getNextException to get them.
Ok, so it's not a problem, the nested exception trace is just printed to
System.err for some reason.
Any way I can stop that from happening?
-andreas
Are you sure it's happening even if you catch the exception? Note that
in your sample programs you were either printing the exception yourself
or letting Java itself do it.