Replace multiple try catches with a single global try catch block [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am trying to aim for a global try catch exception handler as I have several child class
methods that have try catches inside them. So basically I want the parent class method to catch all those
exceptions and log it.
One of the common ones are SQL exceptions.
What is the best approach here to create a common handler which will catch the errors?
Here is an example of what is going on in my application
public class Parent
{
public void ParentMethod()
{
try
{
var childClass = new Child();
var process = childClass.Process();
if (process)
{
// Do this
}
else
{
// raise new Exception
}
}
catch(Exception ex){
WriteToErrorLogger.Error(ex)
}
}
}
public class Child
{
public bool Process()
{
try{
// Do something and save to Database
}
catch (SqlException sqlEx)
{
// log exception
return false;
}
catch (Exception ex)
{
// log exception
return false;
}
}
}

What do you mean? What would make sense would be to do the exact opposite
public class Parent
{
public void ParentMethod()
{
try
{
var childClass = new Child();
var process = childClass.Process();
if (process)
{
// Do this
}
else
{
// raise new Exception
}
}
catch (SqlException sqlEx)
{
WriteToErrorLogger.Error(ex);
}
catch(Exception ex){
WriteToErrorLogger.Error(ex);
}
}
}
Child
public class Child
{
public bool Process()
{
// Do something and save to Database
}
}
If you want to conditionally do it, you could change the child like this
public class Child
{
public bool Process(bool rethrow = false)
{
try{
// Do something and save to Database
}
catch (SqlException sqlEx)
{
if(rethrow) throw;
// log exception
return false;
}
catch (Exception ex)
{
if(rethrow) throw
// log exception
return false;
}
}
}

Related

Missing Exception if thrown in finally [duplicate]

This question already has answers here:
What happens if a finally block throws an exception?
(11 answers)
Exception thrown in catch and finally. CLR behavior vs. try-catch block [duplicate]
(2 answers)
Closed 3 months ago.
I have a code:
class Program
{
public static void Main()
{
try
{
Execute();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void Execute()
{
try
{
Step3();
}
catch (Exception ex)
{
Console.WriteLine("catch");
throw new Exception("1");
}
finally
{
Console.WriteLine("finally");
throw new Exception("2");
}
}
public static void Step3()
{
try
{
Console.WriteLine("Step 3");
throw new Exception("step 3");
}
finally
{
Console.WriteLine("Step 3 finally");
}
}
}
and output:
Step 3
Step 3 finally
catch
finally
2
I do not understand what happened with exception throw new Exception("1");. Did it just disappear? Why?
I have read answer about specification but I'm not sure I understand what happened with throw new Exception("1"); Is it still in memory?

How to throw and catch an exception

I fully accept that this is essentially a repeat of question of
Catching custom exception in c#
That question is closed, so I hope to rephrase it as I am having the same problem.
I have a class that can be summarised thus..
[Serializable()]
public class DataFile : ISerializable
{
public DataFile()
{
// Data structures
}
public DataFile(SerializationInfo info, StreamingContext ctxt) : this()
{
if(true)
{
throw new VersionNotFoundException();
}
// Load data
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
// Save data
}
}
In my MainForm, I have a method that constains code equivilant to..
private DataFile Data;
private string CurrentFile = "C:\myfile.xyz";
private void LoadData()
{
try
{
using (Stream stream = File.Open(CurrentFile, FileMode.Open))
Data = (DataFile)new BinaryFormatter().Deserialize(stream);
}
catch (VersionNotFoundException e)
{
// never gets here
}
catch (Exception e)
{
// VersionNotFoundException gets caught here as an inner exception
}
}
My question(s)
Why would the VersionNotFoundException not get caught in the "catch (VersionNotFoundException e)" section (have I not added it to the top of the exception stack)? What am I doing wrong and how do I fix it? Why/how am I making an 'inner' exception and how do I stop it?
I was scratching my head with this and completely missed the comment.
// VersionNotFoundException gets caught here as an inner exception
You cannot catch inner exceptions like this, however you can use when in C#6 or later
try
{
}
catch (Exception e) when (e.InnerException is VersionNotFoundException e2)
{
Console.WriteLine(e2.Message);
}
catch (Exception e)
{
}
Demo here

How do I handle a exception in a library in C#

When I run the code it goes to the throw statement on the catch part rather at the place where the user calls it even after re-throwing.If i didn't catch and rethrow it goes into the function.How to prevent getting into the class library code and show the exception at the user level and prevent the user to view the library code.
public void Services(string hostName)
{
try
{
if (hostName!=null)
{
}
else
{
FunctionThatThrowException(hostName);
}
}
catch(Exception e)
{
throw;
}
}

How to catch two exceptions in one stack when use using

class Program
{
static void Main(string[] args)
{
try
{
using (var ss = new extest()) {
throw new Exception("Exception1");
}
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
class extest : IDisposable
{
public void Dispose()
{
throw new Exception("Exception2");
}
}
Run the codes result is "Exception2",
So I want to know how you can catch two exceptions, or just catch an Exception1.
My project has thousands of such using, which does not add try, but extest's Dispose is only one place, and I hope to know what exception has thrown before the Dispose.
Thanks
The problem in your example is that the second exception is thrown while the first exception is being handled. I.e. the using statement is effectively a try/finally pair, with the call to Dispose() in the finally block. So, the second exception supersedes the first one.
Having a Dispose() method that throws an exception is a very bad idea. So, the best solution here is to fix that. Don't throw an exception from a Dispose() method. But if you can't fix that for some reason and you want to see both, you need to make sure you're in a position to catch both. You can do this by adding another try/catch inside the using:
try
{
using (var ss = new extest()) {
try
{
throw new Exception("Exception1");
}
catch (Exception exInner)
{
System.Console.WriteLine(ex.Message);
throw;
}
}
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message);
}
The easiest way to handle this would be to rearrange your code:
static void Main(string[] args)
{
try
{
using (var ss = new extest())
{
try
{
CodeThatMightThrowAnException();
}
catch (Exception e)
{
// Process Exception here
}
}
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
Edit:
If the handling of the exceptions inside the using is always going to be the same, you could build a helper class that could make refactoring easier:
public class TryCatchHelper
{
public Exception Exception { get; private set; } = null;
public void Execute(Action action)
{
try
{
action()
}
catch (Exception e)
{
exception = e;
}
}
}
Then in your method:
static void Main(string[] args)
{
var helper = new TryCatchHelper();
try
{
using (var ss = new extest())
{
helper.Execute(() => {
// Your Code Block Here
});
}
}
catch(Exception ex)
{
// The Dispose threw an exception
}
if (helper.Exception != null)
{
// Handle the exception from the block here.
}
}
it's impossible to catch more than 1 exception.
when you throw Exception2 it should be catched in your catch clause. when you see "Exception2" it is printed by System.Console.WriteLine(ex.Message);. So, you can change the log in catch, or change the throwing exception message in Dispose.
reference added:
class Program
{
static void Main(string[] args)
{
try
{
using (var ss = new extest()) {
...
}
}
catch(Exception ex)
{
System.Console.WriteLine("extest error : " + ex.Message);
}
}
}
class extest : IDisposable
{
public void Dispose()
{
throw new Exception("Dispose failed: reason");
}
}

Stop execution of all methods from child method

I am working with a few methods that are called from within other methods, but need to stop processing both methods if an event occurs in the one called from the parent. An example of what I am doing in code would be this:
private void parentMethod()
{
//Do work here
childMethod();
//Do more work here
}
private void childMethod()
{
//Do work (not child labor)
using (var form = new choice(myList))
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
int val = form.listIndex;//values preserved after close
//Do something here with these values
string server = myList2[val - 1];
MyList.Clear();
MyList.Add(server);
}
else
{
Exception e = new Exception("Dialog force closed.",null);
throw e;
}
}
So as you can see here, I tried creating an exception to throw; however, because there are number of other methods getting called from the parent method which also can throw exceptions, but can allow the rest of the code to execute, and the parent method in this example is being called from another method that needs to be stopped as well, how do you stop the execution of multiple methods from within a child method, other that doing Application.Close()?
You need to be more specific in your exception catching. In general, a bare catch is bad practice anywhere except the very top level, as is catch (Exception e) or similar. You should determine which exceptions a method can throw, and then only catch those. Other exceptions will then be passed back up the call stack.
For instance, if you have a method A() that might throw an InvalidOperationException, B() that might throw an ArgumentOutOfRangeException or ArgumentNullException, and C() that might throw an AccessViolationException, your code should look like
public int Main()
{
try
{
try
{
A()
}
catch(InvalidOperationException e)
{
//handle
}
try
{
B()
}
catch(ArgumentOutOfRangeException e)
{
//handle
}
catch(ArgumentNullException e)
{
//handle
}
try
{
C()
}
catch(AccessViolationException e)
{
//handle
}
}
catch (Exception e)
{
//handle all other exceptions
}
}
Now, if any method out of A,B, and C throws an exception you're not expecting, it will be handled by the final catch block.
If you really must have an exception that can only be handled at the top level, it might be a good idea to create your own Exception class:
class MyApplicationException : Exception { }
Then, as long as you never use catch (Exception) anywhere other than at the top level, you will be fine.

Categories

Resources