No Exception thrown for obvious error
See original GitHub issueDriver version
6.4.0.jre9, 7.1.1.jre10-preview
SQL Server version
Microsoft SQL Server 2017 (RTM-GDR) (KB4293803) - 14.0.2002.14 (X64) Jul 21 2018 07:47:45 Copyright © 2017 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 17134: ) (Hypervisor)
also tested on Microsoft SQL Server 2012 (SP4) (KB4018073) - 11.0.7001.0 (X64) Aug 15 2017 10:23:29 Copyright © Microsoft Corporation Developer Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
Client Operating System
Windows 10 Professional
JAVA/JVM version
openjdk version “11” 2018-09-25 OpenJDK Runtime Environment 18.9 (build 11+28) OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
also tested using openjdk version “10.0.2” 2018-07-17 OpenJDK Runtime Environment 18.3 (build 10.0.2+13) OpenJDK 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
Table schema
Not necessary for this test class. Any database connection is sufficient.
Problem description
- Expected behaviour:
I expected this erroneous SQL code
SELECT 1 / 0
to raise an exception and not to finish as if everything has been executed OK. - Actual behaviour: The code finishes without raising an exception. For a real world application this would result in unexpected behavior since some statements could fail without a user noticing.
- Error message/stack trace: n/a
- Any other details that can be helpful:
In the code below some test statements have been included.
- Statement 1: simple conversion error
- Statement 2: simple division by zero error
- Statement 3: division by zero error wrapped in try-catch-block. This statement is the only one which raises an Exception.
Reproduction code
test.java
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
public class test {
private static Connection getConnection() throws SQLException {
final Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost;DataBaseName=Test",
"user", "password");
connection.setAutoCommit(false);
return connection;
}
public static void main(final String[] args) throws SQLException {
try (final Connection connection = getConnection()) {
// Statement 1
// final String sql = "SELECT CONVERT(int, 'some text');";
// Statement 2
final String sql = "SELECT 1 / 0;";
// Statement 3
// final String sql =
// " DECLARE @message nvarchar(max);" +
// " BEGIN TRY\r\n" +
// " SELECT 1 / 0;\r\n" +
// " END TRY\r\n" +
// " BEGIN CATCH \r\n" +
// " SET @message = ERROR_MESSAGE()\r\n" +
// " RAISERROR (@message, 18, 2) WITH SETERROR\r\n" +
// " END CATCH";
try (final Statement stmt = connection.createStatement()) {
boolean hasResults;
try {
hasResults = stmt.execute(sql);
}
finally {
SQLWarning warning = stmt.getWarnings();
while (warning != null) {
System.out.println(warning.getMessage());
warning = warning.getNextWarning();
}
}
processResults(stmt, hasResults);
}
connection.commit();
}
}
private static void processResults(final Statement statement, boolean hasResults) throws SQLException {
while (hasResults || (statement.getUpdateCount()) != -1) {
hasResults = statement.getMoreResults();
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>some.group</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Configuration for maven-compiler-plugin -->
<java.version>11</java.version>
<!-- <java.version>10</java.version> -->
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<!-- <version>6.4.0.jre9</version> -->
<version>7.1.1.jre10-preview</version>
</dependency>
</dependencies>
</project>
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
One of my Stack Overflow answers might be of interest. Specifically, the “error inside ResultSet” in the example code.
hi @AchimHentschel,
Upon further investigation I’ve discovered this behavior is actually as designed. The reason is that “select 1/0” is actually a valid query ie it does not result in a compile time error which would generate an exception on the client side. So in this case you won’t see any errors until the query is run and the error is returned from the server. ie You will need to get the result set and call next() to advance the cursor to see the exception.
In your example, currently processResults does nothing. If you add the code in there to get the result set and move the cursor:
and move the call inside the try block and catch an exception:
you will see the expected exception caught:
Hope this helps, let us know if you require more assistance.