Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do most exceptions omit instance-specific information?

Tags:

c#

.net

exception

I've noticed that most exception messages don't include instance-specific details like the value that caused the exception. They generally only tell you the "category" of the error.

For example, when attempting to serialize an object with a 3rd. party library, I got a MissingMethodException with message:

"No parameterless constructor defined for this object."

In many cases this is enough, but often (typically during development) a message like

"No parameterless constructor defined for this object of type 'Foo'."

can save a lot of time by directing you straight to the cause of the error.

InvalidArgumentException is another example: it usually tells you the name of the argument but not its value. This seems to be the case for most framework-raised exceptions, but also for 3rd party libraries.

Is this done on purpose?

Is there a security implication in exposing an internal state like the "faulty" value of a variable?

like image 722
Francesco De Vittori Avatar asked Dec 12 '11 15:12

Francesco De Vittori


People also ask

What is the main reasons for exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.

Why should you trap for specific exception types?

Catching specific exceptions allows you to handle specific scenarios that you know how to fix.

Why are exceptions better than assertions?

Exceptions versus assertions It doesn't represent a condition that the program has to recover from at run time. An assert stops execution at the statement so that you can inspect the program state in the debugger. An exception continues execution from the first appropriate catch handler.

What are the reasons that cause an exception?

11.1 The Causes of Exceptionsevaluation of an expression violates the normal semantics of the language, such as an integer divide by zero, as summarized in §15.6. an error occurs in loading or linking part of the program (§12.2, §12.3) some limitation on a resource is exceeded, such as using too much memory.


2 Answers

Two reasons I can think of:

Firstly, maybe the parameter that threw the exception was a value that was a processed form of the one that was passed to the public interface. The value may not make sense without the expense of catching to rethrow a different exception that is going to be the same in most regards anyway.

Secondly, and more importantly, is that there can indeed be a security risk, that can be very hard to second-guess (if I'm writing a general-purpose container, I don't know what contexts it will be used in). We don't want "Credit-Card: 5555444455554444" appearing in an error message if we can help it.

Ultimately, just what debug information is most useful will vary according to the error anyway. If the type, method and (when possible) file and line number isn't enough, it's time to write some debug code that traps just what you do want to know, rather than complaining that it isn't already trapped when next time you might want yet different information (field state of instances can be just as likely to be useful as parameters).

like image 64
Jon Hanna Avatar answered Oct 15 '22 01:10

Jon Hanna


InvalidArgumentException and (per @Ian Nelson) "Key not found in dictionary" both share something in common - there's no guarantee that the framework would be able to find a suitable value to show you - if the key/argument is of any user defined type, and ToString() hasn't been overridden, then you would just get the type name - it's not going to add a lot of value.

like image 25
Damien_The_Unbeliever Avatar answered Oct 15 '22 01:10

Damien_The_Unbeliever