# try..catch blocks

**URL:** https://forum.servoy.com/t/try-catch-blocks/2811
**Category:** Classic Servoy
**Created:** [September 30, 2004, 3:05pm UTC](https://forum.servoy.com/t/try-catch-blocks/2811 "2004-09-30T15:05:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anton.dorrestijn](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anton.dorrestijn](https://forum.servoy.com/u/anton.dorrestijn)
#### Post date: [September 30, 2004, 3:05pm UTC](https://forum.servoy.com/t/try-catch-blocks/2811/1 "2004-09-30T15:05:24Z")

</div>

Hello,

When the attached code is executed, an error is thrown on the controller.saveData() method. This is because the primary key has a duplicate value.

Instead of going to the catch block, servoy displays an messagebox with an error on the screen. The method that calls this code thinks it executes okay…

What I want is that the method that calls this code knows that there was an error creating the record! Does anybody know a sollution or work-around for this problem?

Thanks,

Anton

```auto
try
{
	controller.newRecord( false )
	
	try
	{
		contactid = arguments[0]
		relatieid = arguments[1]
	
		//create bug... set duplicate pk
		relcpid = 7;
		
		controller.saveData();
	}
	catch( error )
	{
		controller.deleteRecord();
		throw error;
	}
}
catch( error )
{
	throw error;
}

```

---

<div class="post-metadata">

### Author: ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)
#### Post date: [October 12, 2004, 2:44pm UTC](https://forum.servoy.com/t/try-catch-blocks/2811/2 "2004-10-12T14:44:38Z")

</div>

we can’t throw that error all the way down currently because then the normal save/newrecord behaviour outside a script wouldn’t work at all.

Maybe we can fix this with application.getLastErrorCode() ect.  
Have to check this out.

---

<div class="post-metadata">

### Author: ![anton.dorrestijn](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anton.dorrestijn](https://forum.servoy.com/u/anton.dorrestijn)
#### Post date: [October 12, 2004, 8:19pm UTC](https://forum.servoy.com/t/try-catch-blocks/2811/3 "2004-10-12T20:19:26Z")

</div>

Hello Johan,

Thanks for your reply.

Is there an alternative that I can use **now** to read or catch errors that occured in the data-layer?

Anton

---

<div class="post-metadata">

### Author: ![Jan\_Aleman](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jan\_Aleman](https://forum.servoy.com/u/Jan_Aleman)
#### Post date: [October 20, 2004, 10:58am UTC](https://forum.servoy.com/t/try-catch-blocks/2811/4 "2004-10-20T10:58:38Z")

</div>

At the moment you’d do a pre-validation instead of post (by catching the error). We plan to support post in the future.

---

<div class="post-metadata">

### Author: ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)
#### Post date: [December 4, 2004, 8:48pm UTC](https://forum.servoy.com/t/try-catch-blocks/2811/5 "2004-12-04T20:48:46Z")

</div>

What are try, catch and throw? I can’t seem to find them in the SOM.

Just looking for a pointer to documentation here.

---

<div class="post-metadata">

### Author: ![bcusick](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@bcusick](https://forum.servoy.com/u/bcusick)
#### Post date: [December 5, 2004, 3:42pm UTC](https://forum.servoy.com/t/try-catch-blocks/2811/6 "2004-12-05T15:42:30Z")

</div>

Chris,

TRY and CATCH are Javascript Error Handling objects. When you write Javascript code (and other languages) you use TRY/CATCH to see if there was an error during execution.
