How can I pass on an Exception? - c#

Basically, what I want to do is pass a specific Exception to a more general Exception within the same try block. I've tried the following and it doesn't work:
static bool example(int count = 0)
{
try
{
work();
}
catch (TimeoutException e)
{
if (count < 3)
{
Console.WriteLine("Caught TimeoutException: {0}", e.Message);
return example(count + 1);
}
else
{
throw new Exception(e.Message);
}
}
catch (Exception e)
{
Console.WriteLine("Caught Exception: {0}", e.Message + " rethrown");
return false;
}
return true;
}
static void work()
{
throw new TimeoutException("test");
}
I want the TimeoutException to be only handled a certain amount of times before going to a more generic Exception. This is because the TimeoutException has additional information about the exception on a case by case basis. I do not want to duplicate the code for Exception under the else clause of TimeoutException. The reason I want all exceptions to be handled is that there may be other unknown exceptions that are thrown. The nature of the program requires it to not crash so I must account for any other exceptions and log them. How can I implement this?

You would need to nest this as 2 tries if you want to handle this this way:
static bool example(int count = 0)
{
try
{
try
{
work();
}
catch (TimeoutException e)
{
if (count < 3)
{
Console.WriteLine("Caught TimeoutException: {0}", e.Message);
return example(count + 1);
}
else
{
// Just throw, don't make a new exception
throw; // new Exception(e.Message);
}
}
}
catch (Exception e)
{
Console.WriteLine("Caught Exception: {0}", e.Message + " rethrown");
return false;
}
return true;
}
The "inner try/catch" will only catch TimeoutException, so any other exception will always go to the outer scope. When you rethrow, it'll automatically get caught by the outer scope, as well, which eliminates the need for killing the exception information. (If you throw new Exception, you lose your stack trace data, and other very valuable debugging information.)

Here's my take:
bool example()
{
// Attempt the operation a maximum of three times.
for (int i = 0; i < 3; i++)
{
try
{
work();
return true;
}
catch (Exception e)
{
Console.WriteLine("Caught exception {0}", e.Message);
// Fail immediately if this isn't a TimeoutException.
if (!(e is TimeoutException))
return false;
}
}
return false;
}
EDIT
If you want to actually do something with the TimeoutException, you could change the catch block like so:
catch (Exception e)
{
// As Reed pointed out, you can move this into the if block if you want
// different messages for the two cases.
Console.WriteLine("Caught exception {0}", e.Message);
TimeoutException timeoutException = e as TimeoutException;
if (timeoutException != null)
{
// Do stuff with timeout info...
}
else
{
// Not a timeout error, fail immediately
return false;
}
}

Related

Catch exceptions in multi threads and processes program

I'm trying to catch some specific exception. The problem is that my program is complex and can hide this exception as a 'primary' exception or as an inner exception inside an aggregated exception. (it could also be an inner of an inner exception and so on)
How do I catch this in an elegant way?
What I've done so far
catch (Exception e) when (e.InnerException is MyOwnSpecialErrorException)
{
var e1 = e.InnerException as MyOwnSpecialErrorException;
success = false;
exception = e1;
}
and also:
catch (MyOwnSpecialErrorException e)
{
success = false;
exception = e;
}
Of course here I only take care in the case of one nested inner exception, and also this assumes that it is the first inner exception. Can someone think of some way?
That's the more concise syntax I can think of, featuring the is operator:
bool success;
MyOwnSpecialErrorException exception;
try
{
//...
}
catch (AggregateException aex)
when (aex.Flatten().InnerExceptions.OfType<MyOwnSpecialErrorException>()
.FirstOrDefault() is MyOwnSpecialErrorException myEx)
{
success = false; exception = myEx;
}
catch (MyOwnSpecialErrorException ex)
{
success = false; exception = ex;
}
catch
{
success = true; // Ignore all other exceptions
}

Implement retry mechanism on main thread

How would you implement an unlimited retry mechanism in case internal logics fails
something like this, but here you have only one change
static void Main(string[] args)
{
ILog Log = LogManager.GetLogger(typeof(Program));
try
{
StartWorking(Log);
}
catch (Exception ex)
{
Log.Error("Main exited with error: {0}. Restarting app", ex);
Thread.Sleep(5000);
StartWorking(Log);
}
}
private static void StartWorking(ILog Log)
{
Foo t = new Foo();
t.ReadConfiguration();
t.Login();
t.StartWorking();
}
You could use a while loop:
while (true)
{
try
{
StartWorking(Log);
// No exception was thrown, exit the loop
break;
}
catch (Exception ex)
{
Log.Error("Main exited with error: {0}. Restarting app", ex);
Thread.Sleep(5000);
}
}
Note however that this is very bad practice. You definitely don't want to be doing this. Instead you should have a retry logic that after a number of retries just gives up. For example:
const int maxRetries = 5;
for (var i = 0; i < maxRetries; i++)
{
try
{
StartWorking(Log);
// No exception was thrown, exit the loop
break;
}
catch (Exception ex)
{
Log.Error("Main exited with error: {0}. Restarting app", ex);
Thread.Sleep(5000);
}
if (i == maxRetries - 1)
{
throw new Exception("Sorry, we have reached the maximum number of retries for this operation, just giving up");
}
}

C# Determinig if exception initialized with a message

Is there a way when catching an exception to determine if it were constructed with a non-default message.
try
{
throw new Exception(message); // case 1
//throw new Exception(); // case 2
}
catch(Exception exp)
{
/* what do I put here such that if the case 2 exception were
caught it would output exp.ToString() instead of exp.Message? */
textBox1.Text = exp.Message; // case 1 handeling
}
Just to clarify when Exception(message) is thrown I want it to ouptut exp.Message and when Exception() is thrown I want to output exp.ToString(). I would prefer to accomplish this without adding a custom exception. Thanks.
You need to check the message against a default exception
catch (Exception e)
{
bool isDefaultMessage = e.Message == new Exception().Message;
}
Update
Difference types of Exception
catch (Exception e)
{
bool isDefaultMessage = false;
try
{
var x = (Exception) Activator.CreateInstance(e.GetType());
isDefaultMessage = e.Message == x.Message;
}
catch (Exception) {} // cannot create default exception.
}

How to pass a thrown exception from a C# class to a C# Form

I have a project in c# which is split into UI layer and Business layer.
Basically I have a form where you can select an account and input a number for deposit. Once you click the OK button, your DepositTransaction.cs will handle the transaction.
Here is the sample code for DepositForm:
private void buttonOK_Click(object sender, EventArgs e) {
try {
bool inputTest;
decimal amount;
inputTest = decimal.TryParse(textBoxAmount.Text, out amount);
if (inputTest == false) {
throw new InvalidTransactionAmtException();
} else {
BankAccount account = comboBoxAccount.SelectedItem as BankAccount;
deposit = new DepositTransaction(account, amount);
this.DialogResult = DialogResult.OK;
}
} catch (InvalidTransactionAmtException ex) {
errorProviderDeposit.SetError(textBoxAmount, ex.Message);
textBoxAmount.Select();
textBoxAmount.SelectAll();
} catch (InvalidTransactionAmtNegativeException ex) {
errorProviderDeposit.SetError(textBoxAmount, ex.Message);
textBoxAmount.Select();
textBoxAmount.SelectAll();
} catch (AccountInactiveException ex) {
errorProviderDeposit.SetError(textBoxAmount, ex.Message);
textBoxAmount.Select();
textBoxAmount.SelectAll();
}
}
And now the sample code for the DepositTransaction
public override void DoTransaction() {
try {
if (Amount <= 0) { //Amount is the amount passed by the form
throw new InvalidTransactionAmtNegativeException();
}
if (acc.Active == false) { //acc is the account passed by the form
throw new AccountInactiveException();
}
acc.Credit(Amount);
Summary = string.Format("{0} {1}", Date.ToString("yyyy-MM-dd"), this.TransactionType);
this.setStatus(TransactionStatus.Complete);
} catch (InvalidTransactionAmtNegativeException ex) {
throw;
} catch (AccountInactiveException ex) {
throw;
}
}
However, trying the above, does not pass the error to the Form. It just crashes the program saying that the exception was not handled.
I saw another question on stackoverflow that mentioned the way to pass the error is just to use throw: and that error will be passed to the class that called this class (in my case the form), and it will be handled in the form.
What am I doing wrong?
Thank you
It just means that an exception that is neither of type InvalidTransactionAmtNegativeException nor AccountInactiveException is being thrown. Add new catch block
catch (Exception ex) {
throw;
}
EDIT: You should have it come last. It will catch any other exceptions that might be thrown within your DoTransaction method
You are repeating code in all your catch blocks in the UI, just use a generic catch block:
private void buttonOK_Click(object sender, EventArgs e) {
try {
bool inputTest;
decimal amount;
inputTest = decimal.TryParse(textBoxAmount.Text, out amount);
if (inputTest == false) {
throw new InvalidTransactionAmtException();
} else {
BankAccount account = comboBoxAccount.SelectedItem as BankAccount;
deposit = new DepositTransaction(account, amount);
deposit.DoTransaction();
this.DialogResult = DialogResult.OK;
}
//catch any type of exception here
} catch (Exception ex) {
errorProviderDeposit.SetError(textBoxAmount, ex.Message);
textBoxAmount.Select();
textBoxAmount.SelectAll();
}
}
It Seems that your exception doesn't comes under the specific exception you have given in catch block. So catch generic exception at the end. It is a good practice.

Returning a bool and rethrowing an exception

Is it possible to return a bool and also rethrow an exception within the same method? Ive tried with the following code and it keeps saying that unreachable code is detected or that i cant exit the finally block.
public bool AccessToFile(string filePath)
{
FileStream source = null;
try
{
source = File.OpenRead(filePath);
source.Close();
return true;
}
catch (UnauthorizedAccessException e)
{
string unAuthorizedStatus = "User does not have sufficient access privileges to open the file: \n\r" + filePath;
unAuthorizedStatus += e.Message;
MessageBox.Show(unAuthorizedStatus, "Error Message:");
throw;
}
catch (Exception e)
{
string generalStatus = null;
if (filePath == null)
{
generalStatus = "General error: \n\r";
}
else
{
generalStatus = filePath + " failed. \n\r";
generalStatus += e.Message;
}
MessageBox.Show(generalStatus, "Error Message:");
throw;
}
finally
{
if (source != null)
{
source.Dispose();
}
}
}
Once you throw an exception, processing in your current method finishes and the exception works up the call stack. Either handle your exceptions locally and then return your boolean, or throw them and let them bubble up and handle them at the front end.

Categories