?
Solved

C# throw new exception newline

Posted on 2007-11-14
17
Medium Priority
?
2,395 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Assisted Solution

by:McExp
McExp earned 100 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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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