Solved

C# throw new exception newline

Posted on 2007-11-14
17
2,383 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
Industry Leaders: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
How can I do Unit Testing with Session ? 1 41
Converting PHP triple des decrypt to c# 9 162
Include MS Database in Winforms Project using Intellilock 5 34
Put File 6 36
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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