Please use .ToString() responsibly

I’ve seen this kind of code a lot recently:

try
{
int i = 0;
int x = 0;
Console.WriteLine(i / x);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

This results in the following output:

Attempted to divide by zero.

Unless there’s some compelling reason you need an ultra-terse version of the error, it’s almost always better to use the provided Exception.ToString() method. Compare the difference:

System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication1.Program.Main(String[] args)
in C:Program.cs:line 15

This also brings up an interesting corollary: any object you build should have a meaningful .ToString() method. I expect a proper string representation of what you are, not a meaningless echo of your class name!

One object that violates this horribly is DataSet. Let’s say we have a DataSet containing a single DataTable. When you type DataSet.ToString(), what do you think should happen? Wait, wait, don’t tell me. I’ll tell you. This happens:

System.Data.DataSet

Useless. How about something that actually shows the object in string form?

+----------------------------------------------------------------+
| DataSet1                                                       |
+----------------------------------------------------------------+
| Table1                                                         |
+---------+-----------+------------------+------------+----------+
| field01 | field02   | field03          | field04    | field05  |
+---------+-----------+------------------+------------+----------+
|       1 | first     | NULL             | NULL       | NULL     |
|       2 | second    | another          | 1999-10-23 | 10:30:00 |
|       3 | a third   | more foo for you | 1999-10-24 | 10:30:01 |
+---------+-----------+------------------+------------+----------+

Seems perfectly logical to me, but you’ll have to write your own custom ToString() implementation to get the behavior that should have been there in the first place.

Jeff Atwood

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Let's be kind to each other. Find me https://infosec.exchange/@codinghorror

⏲️ Busy signing you up.

❗ Something's gone wrong. Please try again.

✅ Success! Check your inbox (and your spam folder, just in case).

Related posts

Updating The Single Most Influential Book of the BASIC Era

Updating The Single Most Influential Book of the BASIC Era

In a way, these two books are responsible for my entire professional career. With early computers, you didn’t boot up to a fancy schmancy desktop, or a screen full of apps you could easily poke and prod with your finger. No, those computers booted up to the command line.

By Jeff Atwood ·
Comments
To Serve Man, with Software

To Serve Man, with Software

I didn’t choose to be a programmer. Somehow, it seemed, the computers chose me. For a long time, that was fine, that was enough; that was all I needed. But along the way I never felt that being a programmer was this unambiguously great-for-everyone career field with zero downsides.

By Jeff Atwood ·
Comments
Here’s The Programming Game You Never Asked For

Here’s The Programming Game You Never Asked For

You know what’s universally regarded as un-fun by most programmers? Writing assembly language code. As Steve McConnell said back in 1994: Programmers working with high-level languages achieve better productivity and quality than those working with lower-level languages. Languages such as C++, Java, Smalltalk, and Visual Basic have been credited

By Jeff Atwood ·
Comments
Doing Terrible Things To Your Code

Doing Terrible Things To Your Code

In 1992, I thought I was the best programmer in the world. In my defense, I had just graduated from college, this was pre-Internet, and I lived in Boulder, Colorado working in small business jobs where I was lucky to even hear about other programmers much less meet them. I

By Jeff Atwood ·
Comments

Recent Posts

Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
The Great Filter Comes For Us All

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven’t any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don’t stop there – read the Story of Your Life novella it was based on for so much

By Jeff Atwood ·
Comments
I’m feeling unlucky... 🎲   See All Posts