Author |
Message
|
gaurav.udgir |
Posted: Wed Aug 12, 2009 6:14 am Post subject: Try Catch in ESQL |
|
|
Apprentice
Joined: 18 May 2009 Posts: 32
|
Hi All.
I want to throw a user generated exception instead of MB generated exception. I am trying to get something similar to try catch(as in java) in ESQL. If anyone has any idea, please help.
Thank you in advance.
Regards,
Gaurav |
|
Back to top |
|
 |
Vitor |
Posted: Wed Aug 12, 2009 6:25 am Post subject: Re: Try Catch in ESQL |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
gaurav.udgir wrote: |
I want to throw a user generated exception instead of MB generated exception. I am trying to get something similar to try catch(as in java) in ESQL. |
Given my very hazy understanding of try catch in Java, wouldn't the ESQL statement THROW USER EXCEPTION be the sort of thing you were looking for?  _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
jbanoop |
Posted: Wed Aug 12, 2009 6:42 am Post subject: |
|
|
Chevalier
Joined: 17 Sep 2005 Posts: 401 Location: SC
|
you can use the try-catch & throw nodes in conjuction to catch the MB exception and then throw it as a user defined one.
The THROWS clause can be used from within ESQL to raise an exception in case of a validation faling or something like that. It is usually not a system exception but the message failing some business validations/checks.
In my understanding there is no provision in ESQL "catch" an exception.
connecting the failure terminal of the node to a throw node and then throwing the user exception from there is also an option. |
|
Back to top |
|
 |
mqjeff |
Posted: Wed Aug 12, 2009 6:50 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
jbanoop wrote: |
In my understanding there is no provision in ESQL "catch" an exception. |
DECLARE HANDLER, in v6.1. |
|
Back to top |
|
 |
gaurav.udgir |
Posted: Wed Aug 12, 2009 8:31 am Post subject: |
|
|
Apprentice
Joined: 18 May 2009 Posts: 32
|
Hi,
Thank you for the information. I tried to get the information about the Handler in 6.1 but found that documentation is limited so unable to get how to use it. Could you please help me with an example?
Thank you.
Regards,
Gaurav |
|
Back to top |
|
 |
jbanoop |
Posted: Wed Aug 12, 2009 8:52 am Post subject: |
|
|
Chevalier
Joined: 17 Sep 2005 Posts: 401 Location: SC
|
did you try it out in code ?
Please paste the code that you tried out here and that should act as a good starting point. |
|
Back to top |
|
 |
gaurav.udgir |
Posted: Wed Aug 12, 2009 8:28 pm Post subject: |
|
|
Apprentice
Joined: 18 May 2009 Posts: 32
|
Hi,
I am trying something like this:
DECLARE testDate DATE;
SET testDate = CAST(InputRoot.XMLNSC.Test.LastUpdateDate AS TIMESTAMP FORMAT 'I');
In the above statement I am validating that the date is in valid ISO format or not. Now I want to throw a user exception if the above statement fails i.e. the date is not in valid ISO Format.
Thank you.
Regards,
Gaurav |
|
Back to top |
|
 |
Luke |
Posted: Thu Aug 13, 2009 1:04 am Post subject: |
|
|
Centurion
Joined: 10 Nov 2008 Posts: 128 Location: UK
|
An alternative you might want to consider here is using the DEFAULT parameter in your CAST. You can then check if testDate is set to the DEFAULT value and THROW your own exception.
Of course, if you do it this way, you need to make sure you set the DEFAULT value to something that would never be validly returned by your data. Also, if you're validating a lot of fields, it'll probably be more efficient to use DECLARE HANDLER to catch exceptions.
Cheers |
|
Back to top |
|
 |
adrao45 |
Posted: Thu Aug 13, 2009 2:31 pm Post subject: |
|
|
Novice
Joined: 29 Nov 2004 Posts: 12
|
Quote: |
SET testDate = CAST(InputRoot.XMLNSC.Test.LastUpdateDate AS TIMESTAMP FORMAT 'I'); |
The below code throws an user exception, if the above statement fails to execute and propagates an exception tree to failure teriminal
Code: |
DECLARE EXIT HANDLER FOR SQLSTATE LIKE'S22007' --this is the SQLState if Date time format not valid
BEGIN
THROW USER EXCEPTION VALUES ('The Date format not valid', InputRoot.XMLNSC.Test.LastUpdateDate );
END; |
If you want to catch unclassified exceptions, use "%" for the SQL state on the last handler of a scope. |
|
Back to top |
|
 |
Vinnie |
Posted: Sun Mar 25, 2012 11:00 pm Post subject: Catching Exception in ESQL |
|
|
Newbie
Joined: 05 Mar 2012 Posts: 3
|
If error is thrown using Declare Handler, How to catch the exception in the esql code itself? |
|
Back to top |
|
 |
mqjeff |
Posted: Mon Mar 26, 2012 2:30 am Post subject: Re: Catching Exception in ESQL |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Vinnie wrote: |
If error is thrown using Declare Handler, How to catch the exception in the esql code itself? |
DECLARE HANDLER catches exceptions, it doesn't throw them.
THROW throws exceptions, it doesn't catch them. |
|
Back to top |
|
 |
|