I'm trying to understand how I'm going to use Throw in my code. I have a MainForm class to handle the Windows Form GUI and then I have the Manager class to read and save data from/to files.
I use Try/Catch in both classes, but my instructor want me to use Throw in the Manager class and despite that I'm reading about it, I don't get the point what it will do? Will Throw affect the Try/Catch in the MainForm class?
I also use a message box in the manager class if an exception is catched, but no message box are allow to be in the manager according to the instructor, so how would I do then? Can I use the message box in MainForm class only? Preciate some help to understand and expand my knowledge! Thanks!
MainForm class:
try
{
motelManager.SaveToFile(file);
}
catch
{
MessageBox.Show("Ett fel uppstod!", "Varning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Manager class:
public void SaveToFile(string filePath)
{
try
{
string newFilePath = filePath.Replace(".bin", "");
filestream = new FileStream(newFilePath + ".bin", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(filestream, animals);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Varning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
if (filestream != null) filestream.Close();
}
Throw, and Try...Catch...Finally The try statement defines a code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error.
When your code throws a checked exception, you must either use a try block to catch it, or use the throws keyword on your method to advertise the fact that it throws an exception to any method that may call it, so that it in turn must either use a try block to catch it or use the throws keyword to pass the buck.
Yes, it will catch ApplicationException as it derives from Exception . Handling the base exception should be fine in most cases unless you need to log or do something with a different type of exception... Save this answer.
Try-catch block is used to handle the exception. In a try block, we write the code which may throw an exception and in catch block we write code to handle that exception. Throw keyword is used to explicitly throw an exception. Generally, throw keyword is used to throw user defined exceptions.
your manager class should look like this:
public void SaveToFile(string filePath)
{
try
{
string newFilePath = filePath.Replace(".bin", "");
filestream = new FileStream(newFilePath + ".bin", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(filestream, animals);
}
catch(Exception ex)
{
if (filestream != null) filestream.Close();
throw;
// but don't use
// throw ex;
// it throws everything same
// except for the stacktrace
}
// or do it like this
//catch(Exception ex)
//{
// throw;
// but don't use
// throw ex;
// it throws everything same
// except for the stacktrace
//}
//finally
//{
// if (filestream != null) filestream.Close();
//}
}
and in your main class:
try
{
motelManager.SaveToFile(file);
}
catch (Exception e)
{
MessageBox.Show("Ett fel uppstod!", "Varning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Throw simply raises the exception to the calling function. (in this case, whoever calls SaveToFile). If there is an error handler there, it will be caught, otherwise it will continue up the call stack until it is caught or at the top level.
It is better to handle the Exception in terms of presentation to the user in the Form - simply because in a larger well structured system the Manager object may well not have any connection to the GUI.
General rule is to catch the exception in the backend [Manager] class to cleanup any resources (i.e. close the file) and then re-throw the exception from the exception handler as follows:
public void SaveToFile(string filePath)
{
try
{
string newFilePath = filePath.Replace(".bin", "");
filestream = new FileStream(newFilePath + ".bin", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(filestream, animals);
}
catch(Exception ex)
{
/*
* cleanup resources and rethrow the exception for catching and handling elsewhere
*/
if (filestream != null)
filestream.Close();
throw;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With