Better Epicor Exception Handling in Your Customizations and BPMs

Let’s say you want to do something really stupid:

private void CustomerEntryForm_Load(object sender, EventArgs args)
{
	var result = 10 / (DateTime.Now.Year - 2019);
}

Assuming you are reading this in 2019, this will present us with a lovely exception when we load this form:

Screenshot of an Epicor exception alert - GingerHelp

Props to Epicor for wrapping all of our custom code so, for the most part, no matter what dumb stuff we do the application keeps running.

So if you have been around the .Net block for a while you probably already know how to fix this with a good old try / catch:

try {
	var result = 10 / (DateTime.Now.Year - 2019);
} catch (Exception ex) {
	MessageBox.Show("Go smack your Epicor admin.  They introduced this error into their code: " + ex.Message);
}
Screenshot of an Epicor bpm exception box that shows a much more user friendly error message - GingerHelp

But how about if we don’t want to limit our stupidity to embedded customizations? How do we make an Epicor BPM have the same sort of more helpful exceptions? Just create the same try / catch routine but call the PublishInfoMessage helper:

try {
    // Put your BPM code here
}
catch (Exception ex)  { 
    this.PublishInfoMessage(ex.Message, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", ""); 
} 

With PublishInfoMessage you will just get an informative exception, meaning that it won’t hold up the rest of the code from running. If you want to stop the process dead in the tracks use this instead:

throw new Ice.BLException(ex.Message);

Did this blog help you out at all? If so, please leave me a comment. Same if you have any challenges, I’m here to help! Lastly, if you are looking at this and realizing you want something like it but prefer to have me do it for you - please reach out!