tags:

views:

178

answers:

5

Consider the following example: as part of configuration for my program the user supplies an XML file which should in essence describe an acyclic graph, but my program finds a graph when loading it. This is a critical error, the program cannot continue. What exception should be thrown?

Other examples include trying to load a file in some specific format (say JPEG), but encountering an error along the way; or receiving some data over the network from another 3rd party program which should be correct, but isn't.

In essence - you're parsing some kind of data stream and find an error in it which shouldn't be there and which means that the program cannot continue as expected. What is the right type of exception to throw here?

(Note: This shouldn't be an ArgumentXXXException because this data isn't passed as a parameter to a method).

+3  A: 

Create your own exception - I don't believe there is an exception class in the BCL that will describe exactly this scenario.

Perhaps a CyclicGraphFoundException?

Oded
A: 

You should consider a custom exception or you can also use a FileFormatException.

The exception that is thrown when an input file or a data stream that is supposed to conform to a certain file format specification is malformed.

The FileFormatException is only available in .NET 3.5, 3.0 and requires a reference to WindowsBase.

João Angelo
+1  A: 

Why not provide your own exception that states the application cannot load the configuration.

For your problem streaming JPEG, consider FileFormatException.

I would say it's better to derive as much exception classes as needed to precisely model the situation encountered compared to stacking everything into the same general (too general?!) exception class and only have a message to differentiate these situations.

Gregory Pakosz
ApplicationException is deprecated (sort of): http://msdn.microsoft.com/en-us/library/system.applicationexception.aspx
SealedSun
fair egough, I fixed the answer. thank you
Gregory Pakosz
yeah sort of as you're saying, I quick typed some code in MSVC2005 and as it builds without warnings I didn't figure out they advise not to use `ApplicationException` anymore although they didn't mark it deprecated
Gregory Pakosz
Not the use of ApplicationException is deprecated, but MS suggests not to derive own exception classes from ApplicationException, better directly from System.Exception.
Doc Brown
indeed that's what the link given by @SealedSun says
Gregory Pakosz
+1  A: 

For at least some of your scenarios, System.IO.InvalidDataException would be a good fit.

For an XML file, the best approach is probably to validate against a predefined schema, in which case a System.Xml.Schema.XmlSchemaValidationException validation will be thrown.

Nicole Calinoiu
+1 for finding some existing exceptions.
Ian
You can't validate that a graph has no cycles...
Vilx-
@Vilx: Good point... Are you detecting the cyclicity problem when reading the XML content (i.e.: you would use validation against schema if you could) or when you are constructing the resulting object graph based on previously read data?
Nicole Calinoiu
Currently - when I'm constructing the object graph. I'm not really validating it otherwise (though it probably wouldn't hurt). Also, there are many things that can be validated only in the final phase of object construction. The idea is that the XML describes sort of "objects" which can be based on "templates", which in turn can be based on other "templates", etc. And there is a requirement that the final object has a property "X", but it might come from some template upstream. You can't validate that with any schema, it's just not reasonable.
Vilx-
Accepted your answer, because this is what I used. InvalidDataException. :)
Vilx-
Since your exception occurs during a construction phase rather than during a validation phase, another candidate would be InvalidOperationException (see http://msdn.microsoft.com/en-us/library/ms229007.aspx for usage guidelines). It would be a particularly good choice if a cyclic graph is theoretically OK even though your code can't handle it.
Nicole Calinoiu
A: 

I would follow the MSDN guidelines on Error Raising and handling, in particular

In most cases, use the predefined exception types. Only define new exception types for programmatic scenarios, where you expect users of your class library to catch exceptions of this new type and perform a programmatic action based on the exception type itself...

In your first case, where the file is part of the configuration of your application, I would imagine you wouldn't expect users to catch and handle this specific exception, so I'd use the predefined ConfigurationErrorsException.

In other cases, I'd also try to use a predefined exception unless you expect clients to need to handle this exception differently. The .NET 3.5 FileFormatException as suggested by others seems a good choice. You could also use (abuse?) a type such as System.Data.DataException, even though its documentation doesn't really match your situation. Or a custom exception type if you're not happy with that - but in either case, document the exception types you throw and the circumstances under which you throw them, and I'm sure your users will be happy.

Joe