Michael Weston
Michael Weston

Reputation: 387

How to display only the first line when an exception is raised?

I have a try block in one of my methods. When an exception is raised, I have it display on a label, but every time one is raised, it adds multiple lines of code when I only want to get the first line of the exception without the "at System.String.CompareTo(Object value) at Example.Main()". How do I only get the first line?

Upvotes: 2

Views: 3375

Answers (1)

Willy David Jr
Willy David Jr

Reputation: 9131

You can try this:

catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }

Unless you needed more info on your exception, you can choose from the following properties:

 Console.WriteLine(ex.Source.ToString());
 Console.WriteLine(ex.StackTrace.ToString());
 Console.WriteLine(ex.TargetSite.ToString());

Upvotes: 3

Related Questions