Solved

C# throw new exception newline

Posted on 2007-11-14
17
2,387 Views
Last Modified: 2010-04-21
Hi,
In my Class file I have a function . I am trying to throw an exception with ("/r/n") (newline) but @ my client the second messsage doesnot appear in newline. I also did try  Environment.Newline but dint work

throw new exception ("Customer exists" + ds.tables(0).row["customer_ID"] + "/r/n" + "PlaceExists")
Result : Place exists should be displayed in next line but it doesnt
0
Comment
Question by:dotnet0824
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +3
17 Comments
 
LVL 16

Expert Comment

by:McExp
ID: 20285034
I notice your "\r\n" appears to be the wrong direction, I've used the code bellow sucessfully: -

throw new Exception("This is a test\r\nSecond Line");
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20285064
your code will look more .net stylished like this:
throw new Exception (string.Format("Customer exists {0}\r\nPlaceExists", ds.tables(0).row["customer_ID"]))

0
 

Author Comment

by:dotnet0824
ID: 20285140
whatz that {0}  why is it used
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Assisted Solution

by:McExp
McExp earned 25 total points
ID: 20285180
it is a place marker in a format string, the following statment

string.Format("string {0} \r\n line2 {1} \r\n {0)","XYZ","ABC");

outputs:-

"XYZ
line2 ABC
XYZ"
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20285585
It will be useful for more complex exception messages. If you want more information, you can read the help for the String.Format() method.
0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20285724
msg = "\r\n" should work for stand-alone C# apps.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20286138
2 jaime_olivares

"your code will look more .net stylished ..."

I'd say, more C++ stylished, isn't it?  :)

By the way, to the question here:
Environment.NewLine works on my computer. And I prefer to use it, not "\r\n"
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20286173
>>your code will look more .net stylished ..."
>>I'd say, more C++ stylished, isn't it?  :)

Don't agree, string.Format() is a .Net library feature, it is not related to any language, could be used in C#, C++.net, vb.net, cobol.net, fortran.net, J#, etc, etc.

0
 
LVL 3

Expert Comment

by:yellowjetski
ID: 20287004
Have you tried using single quote?
The hard return is actually only considered one character
Try only adding '\n'

Example "Line 1" + '\n' + "Line 2"
Even this might work Example "Line 1" + '\r' + '\n' + "Line 2"
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287028
why to do that?
consecutive string concatenation with + operator in .net just make your code slower.
0
 
LVL 3

Expert Comment

by:yellowjetski
ID: 20287083
"\r\n" (4 character) is NOT the same as '\r' + '\n' (2 character)

Yes, + operator does take more time but considering this is only for an exception handling process, I don't think creating a StringBuilder is necessary.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287242
>>"\r\n" (4 character) is NOT the same as '\r' + '\n' (2 character)
I think you are totally confused about C# escape sequences, "\r\n" is a string with 2 characters, "\\r\\n" is a string with 4 characters.

>>I don't think creating a StringBuilder is necessary.
No, it is not, that's why string.Format() is a better choice.
0
 

Author Comment

by:dotnet0824
ID: 20288080
Still it dint work... Tried the simplest thing 2 methods given earlier ..
it still prints      "This is a test Second Line "
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int x;
        x = 0;
        try
        {
            if (x == 0)
              //  throw new Exception("This is a test\r\nSecond Line"); METHOD 1 TRIED
                //throw new Exception(string.Format("This is a test\r\nSecond Line"));  METHOD2 TRIED
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }
}
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 100 total points
ID: 20288442
>>Response.Write(ex.Message.ToString());
if you are in a web context, \r\n will be unuseful, you should use a <br> tag:

if (x == 0)
    throw new Exception("This is a test<br>Second Line");
0
 
LVL 16

Expert Comment

by:McExp
ID: 20290870
Where are you outputing your exception? what format is the text output.
0
 

Author Closing Comment

by:dotnet0824
ID: 31409248
Thanks it worked out in asp.net
0
 

Author Comment

by:dotnet0824
ID: 20291306
Thanks it worked out in ASP.NET
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question