System.setErr(new PrintStream(new ByteArrayOutputStream(), false, "UTF-8"));
SQLException ex;
DriverManager.setLogWriter(pw);
ex = new SQLException("A", new Exception("a"));
ex.printStackTrace();
ex.printStackTrace(ps);
ex.printStackTrace(pw);
ex = new SQLException("B");
ex.printStackTrace();
ex.printStackTrace(ps);