Reputation: 9866
I have an SQL exception stack trace to demonstrate this. I want to get all the lines which start with "Caused by:". I don't want to use any Apache APIs for this. How do I do it ?
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:254)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at source.DBUtils.getDBConnection(DBUtils.java:41)
at source.Mups.main(Mups.java:42)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:439)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:454)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:693)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:251)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1140)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
... 8 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:149)
at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:405)
... 13 more
I scanned the SQLException api docs quickly and tried to make some code for my catch section. I got only the first error. What is my mistake ?
catch (SQLException e) {
ArrayList<String> errList = new ArrayList<String>();
errList.add(e.getCause().toString());
SQLException eNew = null;
do{
eNew = e.getNextException();
if(eNew != null){
Throwable th = eNew.getCause();
errList.add(th.toString());
}
}while(eNew != null);
for(String s : errList){
System.out.println(s);
}
e.printStackTrace();
}
Upvotes: 1
Views: 619
Reputation: 16429
You have to recursively look at causes. So, you've caught exception e
, and you can get the cause of that as e1 = e.getCause()
, but then the cause of e1
could be found as e2 = e1.getCause()
, and the cause of e2
as e3 = e2.getCause()
, et cetera.
This is how you can do it -
catch (SQLException e) {
Throwable cause = e.getCause();
while (cause != null) {
System.out.println(cause);
cause = cause.getCause();
}
}
Upvotes: 2
Reputation: 40508
for(Throwable t = e; t != null; t = t.getCause()) errList.add(t.toString());
Should do what you want. It is not likely to be very useful though. Unless you are looking for something specific, the only thing that has any interesting information is usually the original exception, that will be the last item in your list. Everything else is just noise.
Upvotes: 1