In the future, there are a few things you can do to deal with titans. As was suggested, the best first line defense is to make sure that you have some means of shutting your fort off from the outside. A wall and drawbridge is the most effective way against megabeasts. The titan won't be able to smash through the raised drawbridge.
As for killing them, that depends entirely on what they're made of and if they have certain types of attacks. This one has a poisonous bite, which is one of the least dangerous things it could have. Getting into melee with it isn't an immediate death sentence like deadly dust could be.
Another important feature of this titan is that it's organic. It could have been made of metal if you were unlucky. But since it was organic, most weapons would be pretty effective against it. Melee would work alright if you had any well trained soldiers, but you didn't so you lost a lot of dwarves when he started pounding on them (presumably civilians). Crossbows could be quite effective here, as they are against almost anything. I don't think caterpillars have any sort of notion of a skull (although it does mention an exoskeleton), so bolts would probably do a lot of damage and if you're lucky tear its brain apart. My very first titan ever was killed by a hunter who did precisely this.
If the titan had been made of a material like cinnabar or bronze, you would have been in a much tighter spot, but metal bolts do reasonably well against these enemies. Failing that, you might should consider setting up a choke point or airlock system with spikes that can be repeatedly triggered from safety.
In short:
1. Lock yourself in
2. Crossbows (maybe through fortifications)