Solved

C# throw new exception newline

Posted on 2007-11-14
17
2,354 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
  • 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
 
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 29

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now