Solved

C# throw new exception newline

Posted on 2007-11-14
17
2,372 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2 questions 10 30
transaction in asp.net, sql server 6 43
LINQ Select all records by MAX data syntax help 1 19
C# Formatting DateTime String Value 5 35
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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