# \[AS2 MX04\] How to throw/catch errors in callback functions?

**URL:** <https://forum.kirupa.com/t/as2-mx04-how-to-throw-catch-errors-in-callback-functions/137658>\
**Category:** Uncategorized\
**Created:** [February 16, 2005, 3:46pm UTC](https://forum.kirupa.com/t/as2-mx04-how-to-throw-catch-errors-in-callback-functions/137658 "2005-02-16T15:46:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sptm](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sptm](https://forum.kirupa.com/u/Sptm)\
**Post date:** [February 16, 2005, 3:46pm UTC](https://forum.kirupa.com/t/as2-mx04-how-to-throw-catch-errors-in-callback-functions/137658/1 "2005-02-16T15:46:41Z")

</div>

(How) is it possible to catch an error thrown in a callback function like onConnect of the XMLSocket object?

```auto

var s:XMLSocket = new XMLSocket();

// Will not catch error :-(
try {
	s.onConnect = function(succes) {
		trace(succes);
		throw new Error("Unable to connect");
		
		trace("Won't trace, error thrown");
	}
	s.connect("127.0.0.1",1024);
} 
catch(e:Error) {
	trace(e)
}

```
