Servoy’s inbuilt mechanism to handle exceptions is quite effective but there are times when you may want to handle the exception programmatically. For instance, in some situations you may want to customize the Exception Handler to show some custom messages to the user?
Whenever Servoy encounters an error, it throws an exception with the pre-defined message. To add your own customized message, which will be more user friendly, you can attach an Exception Handler to your solution.
Exception Handler in Servoy is a global method which is fired everytime Servoy encounters an error and subsequently the exception is passed to the method as an argument. the Exception will be passed to the method as an argument. You can add the Exception handler to your Solution by assigning the global (Exception Handler) method to the onError event of the solution..
The process may vary slightly in different versions.
[Servoy 3.1+ & 3.5+]
Open the Solution -> Go to File menu -> Solution Settings -> Select the Global Method from the On Error method drop down.
[Servoy 4.0+]
Select the solution -> Go to Properties Tab ->Assign the Global Method to the OnError event.
Sample Exception Handler code
Here is the sample code snippet, that you can add to your Global Method(Exception Handler) to show customized messages to the user.
var svyExpList = new java.util.HashMap();
//Add your message to the respective Exceptions svyExpList.put(ServoyException.ACQUIRE_LOCK_FAILURE, “Acquire Lock Failure”); svyExpList.put(ServoyException.BAD_SQL_SYNTAX, “Bad Sql Syntax”); svyExpList.put(ServoyException.DATA_ACCESS_RESOURCE_FAILURE, “Data Access Failure”); svyExpList.put(ServoyException.DATA_INTEGRITY_VIOLATION, “Data Integrity Violation.”); svyExpList.put(ServoyException.DEADLOCK, “Dead Lock”); svyExpList.put(ServoyException.DELETE_NOT_GRANTED, “Delete not Granted.”); svyExpList.put(ServoyException.EXECUTE_PROGRAM_FAILED, “Execute Program failed.”); svyExpList.put(ServoyException.INCORRECT_LOGIN, “Incorrect login”); svyExpList.put(ServoyException.INVALID_INPUT, “Invalid Input”); svyExpList.put(ServoyException.INVALID_INPUT_FORMAT, “Invalid Input Format”); svyExpList.put(ServoyException.INVALID_RESULTSET_ACCESS, “Invalid Resultset Access”); svyExpList.put(ServoyException.NO_ACCESS, “No Access”); svyExpList.put(ServoyException.NO_CREATE_ACCESS, “No Create Access”); svyExpList.put(ServoyException.NO_DELETE_ACCESS, “No Delete Access”); svyExpList.put(ServoyException.NO_LICENSE, “No License”); svyExpList.put(ServoyException.NO_MODIFY_ACCESS, “No Modify Access”); svyExpList.put(ServoyException.NO_PARENT_DELETE_WITH_RELATED_RECORDS, “No Parent Delete with Related Record”); svyExpList.put(ServoyException.NO_RELATED_CREATE_ACCESS, “No Related Create Access”); svyExpList.put(ServoyException.PERMISSION_DENIED, “Permission Denaid”); svyExpList.put(ServoyException.RECORD_LOCKED, “Record Locked”); svyExpList.put(ServoyException.SAVE_FAILED, “Save Failed”);
svyExpList.put(ServoyException.UNKNOWN_DATABASE_EXCEPTION, “Unknown Database Exception”);
var exception = arguments[0]; //Occurred Exception
if (exception.isServoyException) //Servoy Exception { var msg = svyExpList.get(exception.getErrorCode()); plugins.dialogs.showErrorDialog( “Error Occurred!”, msg, ‘OK’);
}
Add your own messages to each Exception to show the user a proper/custom message.