# Catch getDataSetByQuery return error?

**URL:** <https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550>\
**Category:** Classic Servoy\
**Created:** [April 18, 2009, 11:30am UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550 "2009-04-18T11:30:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 18, 2009, 11:30am UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/1 "2009-04-18T11:30:43Z")

</div>

I’m using the following code to run a SQL query.

dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturnedRows);

This runs SQL stored in a dataprovider and sometimes the SQL will be wrong (badly formed or missing - its only ever a SELECT query BTW). Currently I get the Servoy error that the SQL has an error - but I want to test for this and show my own error dialog. Whats the syntax to check for this error?

Appreciate feedback.

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [April 18, 2009, 1:30pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/2 "2009-04-18T13:30:45Z")

</div>

Hi Ian,

You can get the error from a dataset using the following syntax:

```auto
if ( ds.getExceptionMsg() ) {
     application.output(ds.getExceptionMsg()); // the error message
}

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 18, 2009, 3:42pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/3 "2009-04-18T15:42:53Z")

</div>

> ROCLASI:  
> Hi Ian,
> 
> You can get the error from a dataset using the following syntax:
> 
> ```auto
> if ( ds.getExceptionMsg() ) {
> 
> ```

```
 application.output(ds.getExceptionMsg()); // the error message

```

}

> ```auto
> 
> Hope this helps.
> 
> ```

Thanks for that feedback Robert - Question:

Wont the Error fire and display the Servoy error message before I run this if()?

Should I wrap the entire getDatasetByQuery in this if?

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [April 19, 2009, 6:54am UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/4 "2009-04-19T06:54:50Z")

</div>

Hi Ian,

> Kahuna:  
> Wont the Error fire and display the Servoy error message before I run this if()?
> 
> Should I wrap the entire getDatasetByQuery in this if?

If the error is a SQL error (i.e. an error returned from the database server) then it will never fire an exception in Servoy. You need to actively check if an error was returned.So the IF needs to be after you use the getDataSetByQuery().  
Or are checking for database errors using an onError global method?

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 19, 2009, 9:31am UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/5 "2009-04-19T09:31:47Z")

</div>

> ROCLASI:  
> If the error is a SQL error (i.e. an error returned from the database server) then it will never fire an exception in Servoy. You need to actively check if an error was returned.So the IF needs to be after you use the getDataSetByQuery().  
> Or are checking for database errors using an onError global method?

I see what you’re suggesting Robert but I’m not sure if I’ve misled you on what I’m doing (or if I’m having a particularly dumb day - wholly possible ![:lol:]( "Laughing") )

Heres what I’m doing:

//Get a dataset based on query  
var maxReturnedRows = -1;  
var query = vSQL // Query MUST be well formed - Taken from Threats Form SQL field  
var dataset = ‘’  
dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturnedRows);

Where there is legal SQL this runs as expected. But in my test case with illegal SQL I get the following error:

com.sevoy.j2b.dataprocesing.DataException: You have an error in your SQL syntax…

If after the above I then add:

if ( dataset.getExceptionMsg() ) {  
application.output(dataset.getExceptionMsg()); // the error message}

I still receive the same error message - and the error is NOT output to the console? Is there some way to test the SQL before assigning it to the dataset variable?

Really do appreciate your feedback and help Robert - Cheers

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [April 19, 2009, 6:30pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/6 "2009-04-19T18:30:42Z")

</div>

Hi Ian,

Can you show what SQL gives you this error ?  
Also can you make sure the error comes from that line of code ? Use the debugger to step through it.

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 19, 2009, 7:44pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/7 "2009-04-19T19:44:01Z")

</div>

> ROCLASI:  
> Hi Ian,
> 
> Can you show what SQL gives you this error ?  
> Also can you make sure the error comes from that line of code ? Use the debugger to step through it.

Robert this is really just junk SQL as a test - I know there will be times when the SQL input is wrong.

I’ve run the debugger and it comes from the var setting on databaseManager…

dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturnedRows);

I’ve tried it with just broken SQL: SELECT \* From xyz (where xyz does not exist) and also

SELECT (and no other string)

Essentially the SQL string could be completely rubbish Robert. Does that make sense?

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [April 19, 2009, 9:45pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/8 "2009-04-19T21:45:44Z")

</div>

Hi Ian,

I believe you ![:)]( "Smile")  
You also don’t have any onError global methods in use in your solution ?

You are using Servoy 4.1.1. for this ? If so that this seems to be a new behavior then because up to 3.5.x every SQL error the back-end threw when using the databaseManager you had to check yourself.  
Unless of course the JDBC driver itself was throwing the error. But normally with malformed SQL the driver won’t do this.

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 19, 2009, 11:18pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/9 "2009-04-19T23:18:56Z")

</div>

> ROCLASI:  
> Hi Ian,
> 
> I believe you ![:)]( "Smile")  
> You also don’t have any onError global methods in use in your solution ?
> 
> You are using Servoy 4.1.1. for this ? If so that this seems to be a new behavior then because up to 3.5.x every SQL error the back-end threw when using the databaseManager you had to check yourself.  
> Unless of course the JDBC driver itself was throwing the error. But normally with malformed SQL the driver won’t do this.

I’ve got no other error handling running that could catch this one Robert!

I cant actually see how I could catch the error if I cant check for it post ‘databaseManager…’ unless there is some what to say something like

if(! databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturnedRows))  
{  
error  
}  
???

Perhaps one of the dev team could pick-up on this and comment??

---

<div class="post-metadata">

**Author:** ![rgansevles](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rgansevles](https://forum.servoy.com/u/rgansevles)\
**Post date:** [April 21, 2009, 7:55pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/10 "2009-04-21T19:55:36Z")

</div>

Ian,

You can either catch the sql error in a try-catch construct around the databaseManager.getDataSetByQuery() call or configure a solution onError method (property on Solution) that will be fired on the error.

Rob

---

<div class="post-metadata">

**Author:** ![Kahuna](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@Kahuna](https://forum.servoy.com/u/Kahuna)\
**Post date:** [April 21, 2009, 8:06pm UTC](https://forum.servoy.com/t/catch-getdatasetbyquery-return-error/10550/11 "2009-04-21T20:06:19Z")

</div>

> rgansevles:  
> Ian,
> 
> You can either catch the sql error in a try-catch construct around the databaseManager.getDataSetByQuery() call or configure a solution onError method (property on Solution) that will be fired on the error.
> 
> Rob

Thanks Rob - I’ll try that tomorrow.

Cheers
