Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JDBC exception problem

1 view
Skip to first unread message

Andreas Nordseth

unread,
Mar 4, 2002, 7:05:06 AM3/4/02
to
hi.

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)


Jon Skeet

unread,
Mar 4, 2002, 7:25:24 AM3/4/02
to
Andreas Nordseth <andr...@stud.ntnu.no> wrote:

<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

Andreas Nordseth

unread,
Mar 4, 2002, 8:27:46 AM3/4/02
to

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.16ed78109...@dnews.peramon.com...

> > 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.

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


Jon Skeet

unread,
Mar 4, 2002, 8:56:59 AM3/4/02
to
Andreas Nordseth <andr...@stud.ntnu.no> wrote:
>
> "Jon Skeet" <sk...@pobox.com> wrote in message
> news:MPG.16ed78109...@dnews.peramon.com...
> > > 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.
>
> 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) { }

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.

Andreas Nordseth

unread,
Mar 4, 2002, 10:06:53 AM3/4/02
to

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.16ed8d811...@dnews.peramon.com...

> Andreas Nordseth <andr...@stud.ntnu.no> wrote:
> >
> > "Jon Skeet" <sk...@pobox.com> wrote in message
> > news:MPG.16ed78109...@dnews.peramon.com...
> > > > Why can't I catch the ConnectException???
> > >
> > > 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.
> > I see two exceptions? why??? is the ConnectException in another
thread???
<cut>

>
> 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


Jon Skeet

unread,
Mar 4, 2002, 10:59:54 AM3/4/02
to
Andreas Nordseth <andr...@stud.ntnu.no> wrote:
> I'm not sure i understand, how do i catch the ConnectionException (and the
> SQLException)? can you give me some example code?

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.

Andreas Nordseth

unread,
Mar 4, 2002, 11:04:54 AM3/4/02
to

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.16edaa59...@dnews.peramon.com...

> Andreas Nordseth <andr...@stud.ntnu.no> wrote:
> > I'm not sure i understand, how do i catch the ConnectionException (and
the
> > SQLException)? can you give me some example code?
>
> 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


Jon Skeet

unread,
Mar 4, 2002, 11:46:14 AM3/4/02
to
Andreas Nordseth <andr...@stud.ntnu.no> wrote:
> 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?

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.

0 new messages