In an application with multiple levels, exceptions that occur in the base layers are not sent to a higher level or to the calling application.
For example, if something goes wrong in the code associated with the database, you do not send it to the client application or to a higher level. The reason for this is to provide user friendly error messages. Let's say that during the delete operation you had reference errors of a foreign key, you can:
- Record the exception information.
- Replace it with a user-friendly exception message and move it to the layer above.
The layer above can wrap this exception with another message of a higher level, and then throw it forward. This is similar to what you were asked to do.
In your code in the Manager class, check how many exceptions can occur. If you use VS, the hint / hint text provides this information. If you are not using VS, check out MSDN for this information.
In the form, handle all exceptions that can be selected by the manager layer, as well as a general exception if something terrible happens. IMHO, this is how your code in the manager layer should like
try { string newFilePath = filePath.Replace(".bin", ""); FileStream filestream = new FileStream(newFilePath + ".bin", FileMode.Create); BinaryFormatter b = new BinaryFormatter(); b.Serialize(filestream, animals); } catch (ArgumentNullException argNullException) { // Log current exception // Wrap it under your exception type CustomWrapException customWrap = new CustomWrapException(); customWrap.Message = "Your custom message here."; customWrap.InnerException = argNullException; throw customWrap; } catch (SecurityException securityException) { // Log current exception // Replace current exception with you custom exception CustomReplaceException replaceException = new CustomReplaceException(); replaceException.Message = "Your custom message here."; throw replaceException; } finally { // Close stream and dispose objects here }
Your form must have exception handling, for example:
try { // Call mananger code from here } catch (CustomWrapException wrapException) { // replace/wrap if desired // Display message to user } catch (CustomReplaceException replaceException) { // replace/wrap if desired // Display message to user } catch (Exception exception) { // This is for everything else that may go wrong apart from known possible exceptions // Display message to user } finally { }
NTN.
source share