Jump to content
Xtreme .Net Talk

.ToString() on non specified Exception.HelpLink


Recommended Posts

Posted

I wonder why the .ToString() methode of line 20 failes....

Some more questions;

1/ Is a string actually a value type or a reference type?

2/ If a string is a value type, what is the default initial value for the strings of an array of strings that is instantiated ("", null or String.Empty???)?

3/ And what is the value of the Exception.HelpLink string when not specified?

By the way, ToString() seems to work on String.Empty....

 

[CS]

namespace Tests

{

using System;

 

public class Test

{

public static void Main()

{

try

{

double a = 6;

double b = 0;

Console.WriteLine("{0} / {1} = {2}",a,b,Divide(a,b));

}

catch (DivideByZeroException divByZero)

{

//the folowing line WORKS, HelpLink property is seen as "" (empty string)

Console.WriteLine("Help on error: '{0}'\n\n",divByZero.HelpLink);

//the folowing line THROWS AN EXCEPTION, WHY ????

Console.WriteLine("Help on error: '{0}'\n\n",divByZero.HelpLink.ToString());

}

}

 

public static double Divide(double a, double b)

{

if (b == 0)

{

DivideByZeroException e = new DivideByZeroException("Argument b was 0 in DoDivision (dividing by zero)");

//folowing line is commented to see what the HelpLink property contains by default....

//e.HelpLink = "More about dividing by zero...";

throw e;

}

return a/b;

}

}

 

}

[/CS]

qrt
  • Leaders
Posted
if divByZero.HelpLink is empty ( ie: "" ) , you cant use ToString() because you can't convert " Nothing " to a String , there needs to be a value there. But by the look of it , divByZero.HelpLink is a string based result anyway , so it shouldnt need ToString() on the end of it.

Posted

It shouldn't need it, but an exception shouldn't be thrown either when ToString() is called - a methode overritten by the String class, no harm to call it - and this while...

 

System.Console.WriteLine(System.String.Empty.ToString());

doesn't throw any exceptions.

 

ps:

 

1/ Is a string actually a value type or a reference type?
I know that strings are immutable, and so act like value types when passed to a methode. On the other hand however, System.String doesn't inherit from System.ValueType as value types seem to do... some more info is very welcome....
qrt

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...