question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No Exception thrown for obvious error

See original GitHub issue

Driver 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

  1. 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.
  2. 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.
  3. Error message/stack trace: n/a
  4. 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:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
gordthompsoncommented, Oct 15, 2018

One of my Stack Overflow answers might be of interest. Specifically, the “error inside ResultSet” in the example code.

2reactions
lilgreenbirdcommented, Oct 11, 2018

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:

    ResultSet rs = statement.getResultSet();
    while (rs.next()) {
        System.out.println("result = " + rs.getObject(1));
    }

and move the call inside the try block and catch an exception:

    try {
        hasResults = stmt.execute(sql);
    } catch (Exception e) {
        System.out.println("exception caught: " + e.getMessage());
    }

you will see the expected exception caught:

exception caught: Divide by zero error encountered.

Hope this helps, let us know if you require more assistance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test that no exception is thrown? - java - Stack Overflow
I think an obvious test would be to assert that, when the resulting string is parsed, no exception is thrown. I would write...
Read more >
When to Throw an Exception
Another situation where throwing an exception is appropriate is when the problem occurs in ... It is not always obvious when an exception...
Read more >
Catch block entered even when there is no exception - Question
If catch block was entered, then an exception was thrown. I would try printing the exception message to see why there was an...
Read more >
Throwing an exception in Java - Javamex
At any point where an error condition is detected, you may throw an exception using the Java throw keyword. When an exception is...
Read more >
[Chapter 4] 4.5 Exceptions
An exception indicates an unusual condition or an error condition. Program control becomes unconditionally transferred or thrown to a specially designated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found