Throw a C # exception of the same type as the caught one?

why (if at all) is a bad idea?

class Program { static void Main(string[] args) { try { throw new NotImplementedException("Oh dear"); } catch (Exception ex) { throw NewException("Whoops", ex); } } // This function is the salient bit here public static Exception NewException(String message, Exception innerException) { return Activator.CreateInstance(innerException.GetType(), message, innerException) as Exception; } } 

The important thing here is that the function throws an exception of the same type as the "innerException".

I think ... "Oh ... an exception occurred. I can't handle it here, but I can add more information and throw it. Maybe another handler, above the call, the chain can handle it."

An example would be some SQL error. I may not be able to handle the exception at the time of the call, but I may wish to add additional “contextual” information, for example, “I called this and passed it.”

It seems that it would be useful to pass a backup of the call chain to an exception from the type that was originally created, as opposed to “Exceptions” or “Application Exceptions”. I know that I can create my own exception classes, but it seems like it doesn't add anything when you already have a wonderful specific exception.

Of course, I could be wrong. It can be very useful to do ... but a small voice does not offer.

----- edit -----

For discussion purposes, consider the effects of the following two functions (using the code above):

This ... is too often seen:

  static int SalesTotal(int customerNumber) { try { throw new DivideByZeroException(); // something you didn't expect } catch (Exception ex) { throw new ApplicationException("Unable to calculate sales for customer " + customerNumber, ex); } } 

against it...

  static int SalesTotal(int customerNumber) { try { throw new DivideByZeroException(); // something you didn't expect } catch (Exception ex) { throw NewException("Unable to calculate sales for customer " + customerNumber, ex); } } 
+7
c # exception-handling
source share
7 answers

Creating a new type of exception is not a good option for a general method like this, since existing code will not be able to respond to a specific error. (However, translation exceptions at the API border may be useful).

Creating a new exception of the same type seems dangerous. Is CreateInstance overload used to copy all fields from innerException to a new external exception? What if the exception handler above the stack depends on the parsing of the Message property? What if an exception constructor has side effects?

IMHO, what you are really doing is being recorded, and you will probably be better off taking notes and re-casting.

+4
source share

all exceptions have a Data property to which you can add additional data. There is no need to create a new exception. Just catch the existing exception and add your information, and then just clear the exception.

So you get your cake and eat it too. :)

http://msdn.microsoft.com/en-us/library/system.exception_members(v=VS.90).aspx

+12
source share

I think you should create your own exception type. What information are you going to add? How does an eye catcher learn that it has additional information? If you use a non-standard type, they will have additional properties / methods for viewing or calling.

+3
source share

Do not catch him at all and just let him jump - there is no point in reinventing the wheel.

Not only your method is not intuitive for someone else, but you will also lose the original stack trace from the first exception.

+1
source share

In some cases this may make sense. If you consider MyClass.MyFunction to be publicly available “outside” the assembly, then what happens inside it is a black box for calling the code. Therefore, it may be wiser to have this as the point at which the exception occurred with regard to the calling code, and the most reasonable type of exception may be the same type as the infected one.

I would be careful though. In most cases this happened or you had to catch that the exception was supposed to happen and was thrown proactively (if you are going to throw, the sooner the better), otherwise the exception you throw is really the right type (if your code does not find the file it needs is a FileNotFoundException for you, but if the files are an implementation detail, then this is not a FileNotFoundException exception for the calling code), otherwise the original exception will make perfect sense and it just needs to be allowed or restarted with throw; throw; .

So, not always a bad idea, but a bad one, often enough, that there is always a suspicious thing.

0
source share

IMHO, it would probably be nice to have several custom exception types with one hierarchy for exceptions that say: "This operation failed, but the state of the system seemed to never be an attempt," another for exceptions that say: The operation failed, and the system state has been violated, but it may be crushed ", and another for" The operation failed and recovery failed, the system state cannot be trusted. "In some contexts, one exception class may be caught n and is re-displayed as another (for example, if the operation that was supposed to complete the restoration of the state of the system failed, even if nothing was violated from the point of view of this operation, the restoration of the state failed, it may leave it violated, on the contrary, if the operation left the system the state is violated, but the catch handler restored the state, it could collapse, because "the operation failed, but the state of the system is not violated."

I really do not like the idea of ​​creating exception instances that blindly match the types of excluded exceptions, since abandoned exceptions may have fields that will be handled by handlers. I'm not sure why Microsoft made exceptions "almost" unchanged. It would be much better if the exceptions were immutable and supported cloning with any semantics that convey immutability.

0
source share

Exclude exceptions that you are handling, for example, you do not want the SqlException to be thrown from your data layer to your business layer (catch it, try to handle it and throw a DataLayerException or something similar).

0
source share

All Articles