Reputation: 185
I'm currently trying to have it display another message only if an Exception isn't caught when using Try Catch. Right now it'll display an exception, and then my message after that. So I was just asking for some tips on how I could go about doing that.
Here's my code.
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Solution Downloaded in C:/dwnload/working");
}
Upvotes: 2
Views: 1042
Reputation: 23174
Simple refactor the message as a variable. Assign the success message at the end of the try block.
in the catch block, assign the error message.
Then display the MessageBox, in the finally block or after, as you prefer.
string message;
try
{
// at the end of try block
message = "Solution Downloaded in C:/dwnload/working);
}
catch (Exception ex)
{
message = ex.Message;
}
finally
{
// in general, keep the finally code as the minimum needed for sanity cleanup.
}
// If you rethrow your exception, and still want to show the message box, then might have a reason for wanting this instruction back inside the finally block.
MessageBox.Show(message);
Upvotes: 2
Reputation: 3037
try
{
// Do Work
// when we get here: success! Without errors!
MessageBox.Show("Solution Downloaded in C:/dwnload/working");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Upvotes: 4
Reputation: 38767
You should just do it after the code that can throw an exception in the try
statement.
try
{
// do operation that could throw exception
// display message
}
catch (Exception e)
{
// catch code
}
finally
{
// code to run at the end regardless of success/failure
}
Upvotes: 4