exception

  • Thread starter Thread starter Shane.H.1
  • Start date Start date
S

Shane.H.1

Hi
Im learning C sharp, and I am trying to grok exceptions
Using the following code name is returning "N/A" where I want it to
return the string held by tempHold
What am I doing wrong?

public string Name
{

get{
return name;
}
set{

string tempHold = value;
if(value.Length >14){

//value.Length = -0;
string tempStr = value;
tempHold = "";
for (int i = 0; i <=14;i++){
tempHold += tempStr;
}
value = tempHold;
Exception e = new Exception("Error the name must be less than 14
characters long: name now equals" + tempHold);
throw e;
}
name = value;
}
}
 
How about this:

public string Name
{
get
{
return name;
}
set
{
if (value != null || value.Length < 15)
throw new Exception(...);
name = value;
}
}


You seem to be doing many unnecessary operations and you're missing
several little things. Simplicity breeds success, and a simpler method
will probably give you fewer problems.


Stephan
 
Im learning C sharp, and I am trying to grok exceptions
Using the following code name is returning "N/A" where I want it to
return the string held by tempHold
What am I doing wrong?

Well, you're only setting name if the value that's passed in is valid -
and that seems only reasonabe. An exception is being thrown otherwise.
The exception is thrown instead of the property being set, and that's
usually a useful semantic: it would be quite odd for name to be changed
*and* an exception to be thrown. (Where possible, it's useful for a
method to make no visible changes if it throws an exception.)

You should also look up String.Substring, by the way: your current code
is equivalent to (but less efficient and harder to read than):

public string Name
{
get
{
return name;
}
set
{
if (value.Length > 14)
{
throw new Exception
("Error the name must be less than " +
"14 characters long: name now equals" +
value.Substring (0, 14));
}
name = value;
}
}

Now, if you *do* want the semantics suggested by the text of the
exception, use:

public string Name
{
get
{
return name;
}
set
{
if (value.Length > 14)
{
name = value.Substring(0, 14);
throw new Exception
("Error the name must be less than " +
"14 characters long: name now equals" +
name);
}
else
{
name = value;
}
}
}
 
Huh??
Why are you looping on it?

public string Name
{

set
{

bool badName = false;

if( null==value)
{
badName = true;
}
else
{
if(value.Length >14)
{
badName = true;
}
}

if (badName==true)
{
throw new ArgumentException("Name must be < 14 characters");
}

m_name = value;

}
}


Hi
Im learning C sharp, and I am trying to grok exceptions
Using the following code name is returning "N/A" where I want it to
return the string held by tempHold
What am I doing wrong?

public string Name
{

get{
return name;
}
set{

string tempHold = value;
if(value.Length >14){

//value.Length = -0;
string tempStr = value;
tempHold = "";
for (int i = 0; i <=14;i++){
tempHold += tempStr;
}
value = tempHold;
Exception e = new Exception("Error the name must be less than 14
characters long: name now equals" + tempHold);
throw e;
}
name = value;
}
}
 
Hi
Im learning C sharp, and I am trying to grok exceptions
Using the following code name is returning "N/A" where I want it to
return the string held by tempHold
What am I doing wrong?

public string Name
{

get{
return name;
}
set{

string tempHold = value;
if(value.Length >14){

//value.Length = -0;
string tempStr = value;
tempHold = "";
for (int i = 0; i <=14;i++){
tempHold += tempStr;
}
value = tempHold;
Exception e = new Exception("Error the name must be less than 14
characters long: name now equals" + tempHold);
throw e;
}
name = value;
}
}

ssamuel said:
How about this:

public string Name
{
get
{
return name;
}
set
{
if (value != null || value.Length < 15)
throw new Exception(...);
name = value;
}
}


You seem to be doing many unnecessary operations and you're missing
several little things. Simplicity breeds success, and a simpler method
will probably give you fewer problems.


Stephan
Hi,
Yeah the only thing is I want name updated if its too long
Using your suggestion I could ask the user to re-enter new details but
I want to handle the issue in the code
Also, with your suggestion name is given value, which may be too long
I only want name (in this case) to be 0 - 14 chars long
 
Jon said:
Well, you're only setting name if the value that's passed in is valid -
and that seems only reasonabe. An exception is being thrown otherwise.
The exception is thrown instead of the property being set, and that's
usually a useful semantic: it would be quite odd for name to be changed
*and* an exception to be thrown. (Where possible, it's useful for a
method to make no visible changes if it throws an exception.)

You should also look up String.Substring, by the way: your current code
is equivalent to (but less efficient and harder to read than):

public string Name
{
get
{
return name;
}
set
{
if (value.Length > 14)
{
throw new Exception
("Error the name must be less than " +
"14 characters long: name now equals" +
value.Substring (0, 14));
}
name = value;
}
}

Now, if you *do* want the semantics suggested by the text of the
exception, use:

public string Name
{
get
{
return name;
}
set
{
if (value.Length > 14)
{
name = value.Substring(0, 14);
throw new Exception
("Error the name must be less than " +
"14 characters long: name now equals" +
name);
}
else
{
name = value;
}
}
}

Sweet, thats exactly what needed to be done, as is probably obvious Im
not overly familiar with SubString, but thats how I learn right :-)
 
Yeah the only thing is I want name updated if its too long

Huh? You want it updated *then* you want to throw the exception?

That's easy:

set
{
name = value;
if (name == null || name.Length > 14)
throw new Exception(...);
}

There's almost definitely a better way to implement that logic,
however.

Using your suggestion I could ask the user to re-enter new details but
I want to handle the issue in the code

In a property is never the correct place to ask the user for more or
different information. Your best bet here is to do something more
robust with user input. Try something like getting the input, then
immediately checking if it's valid.

Also, with your suggestion name is given value, which may be too long
I only want name (in this case) to be 0 - 14 chars long

I'm sure you can figure out how to tweak the exact logic (> and <
signs, etc.) to fit what you're trying to do.

Are you sure you understand the symantic meaning of classes and
properties? It seems like you're trying to write procedural (non-OOP)
code and shoehorn it into a class.


Stephan
 
Back
Top