I am developing a database and some JSP Pages to perform CRUD operations on it.
I cannot catch exceptions related to my SQL Code in my JSP Code.
For Example:
I have this part of code where I raise an error in T-SQL:
IF @existing > 0 BEGIN RAISERROR ('ID primário ja existe', 1, 1) RETURN 0 END IF (@CategoriaId IS NULL) BEGIN RAISERROR('O CategoriaId não pode ser nulo',1,1) RETURN 0 END
I need to call the function where I raise those errors in JSP code.
Heres a part of the JSP Code:
try { rs = stmt.executeQuery(sql); } catch (Exception u) { out.println(u.toString()); }
And it never returns the error from T-SQL.
However I know that the function reaches the catch statement because if I throw a println(“test”) in there it writes test on the jsp page.
I always get this exception : >com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
This is an insert operation I’m doing, and if I write the correct values it inserts without any problems. But even when it inserts it throws that exception.
Edit: I tried u.getMessage() already
Advertisement
Answer
You really should read the exception message you get.
The reason you don’t get the exception from the database is probably because it’s masked by the exception you get from the java.
I’m a .net guy myself, the last time I’ve worked with java was about 17 years ago, so I’m not familiar with the correct method you should use, but it’s probably not executeQuery
.
According to this post, you should use execute
instead.
Update
After our conversation in the comments I’ve done some digging and found this pearl in MSDN.
Note this part:
A RAISERROR severity of 11 to 19 executed in the TRY block of a TRY…CATCH construct causes control to transfer to the associated CATCH block. Specify a severity of 10 or lower to return messages using RAISERROR without invoking a CATCH block.
This means that your raiserror
‘s severity parameter should be somewhere within the range of 11 to 19.