I have an MVC EF5 setup, with classes:
Program- this is the controllerUserInterface- this is the view, responsible for displaying and prompting for data.DataAccess- Model, this Creates, Reads, Updates, and Deletes data in my EF model classes
When the DataAccess class tries to do a CRUD operation on my database, if it catches an error, it needs to be handled, my UserInterface class needs to print messages to the user, reporting any errors if neccessary. So, when an error happens, it needs to go through the program class first, then to the UserInterface class, because data layer shouldn't directly communicate to the presentation layer.
It was suggested to me that I don't pass or return the exception to a calling function, but that I should "throw a new simpler exception to the layers above". All this talk about exceptions is confusing to me because My experience with exceptions is limited to this format:
try { // stuff } catch (exception ex) { console.writeline(ex.ToString()); } I've done some of my own research to try and find the answer to this problem, and I've learned a few things but don't know how to put it all together:
I learned:
throw;rethrows an exception and preserves the stack tracethrow exthrows an existing exception, such as one caught in a catch block. and resets the stack trace.- There is a property called Exception.StackTrace. I understand that each time an exception is thrown, the frames in the call stack are recorded to the Exception.StackTrace property.
However, I don't know where to place my try/catch blocks to utilize rethrowing
Is it something like the following code? Or am I missing the point on how this works?
EDITED: (added a little more to make sense of this guesswork to others)
void MethodA() { try { MethodB(); } catch (MyExceptionType ex) { // Do stuff appropriate for MyExceptionType throw; } } void MethodB() { try { MethodC(); } catch (AnotherExceptionType ex) { // Do stuff appropriate for AnotherExceptionType throw; } } void MethodC() { try { // Do Stuff } catch (YetAnotherExceptionType ex) { // Do stuff appropriate for YetAnotherExceptionType throw; } }