473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Exception handling

Hi,

I have a class Test with a property Active. Setter and getter is
implemented.
Setter ~
set
{
try
{
if (SomeValue) throw new Exception("Error");
else FActive = value
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message)}
}

In my main application that uses this Test class I code something like this:
Test aTest = new Test;
aTest.Active = true;
Do other stuff1
Do other stuff2
....

My problem: How do I break execution on the line "aTest.Active = true;" if
the setting of the Active property raised an exception ?
In other words: I do not want to execute "do other stuff1 & 2" if an
exception was raised on the previous line.

I know I can solve this by doing this:
Test aTest = new Test;
aTest.Active = true;
if (aTest.Active)
{
Do other stuff1
Do other stuff2
...
}
, but this does not feel right :)

any hints or help greatly appriciated.

regards,
Leo
Nov 17 '05 #1
7 5111
Setter ~
set
{
try
{
if (SomeValue) throw new Exception("Error");
else FActive = value
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message)}
}

}

In your main application that uses this Test class :

Test aTest = new Test;
try
{
aTest.Active = true;
}
catch
{
throw;
}
Do other stuff1
Do other stuff2

--
http://codebetter.com/blogs/ranjan.sakalley/
"Leif Eirik Olsen" <leif-eirik.olsen@_REMOVE_plugging.com> wrote in message
news:Od**************@TK2MSFTNGP15.phx.gbl...
Hi,

I have a class Test with a property Active. Setter and getter is
implemented.
Setter ~
set
{
try
{
if (SomeValue) throw new Exception("Error");
else FActive = value
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message)}
}

In my main application that uses this Test class I code something like
this:
Test aTest = new Test;
aTest.Active = true;
Do other stuff1
Do other stuff2
...

My problem: How do I break execution on the line "aTest.Active = true;" if
the setting of the Active property raised an exception ?
In other words: I do not want to execute "do other stuff1 & 2" if an
exception was raised on the previous line.

I know I can solve this by doing this:
Test aTest = new Test;
aTest.Active = true;
if (aTest.Active)
{
Do other stuff1
Do other stuff2
...
}
, but this does not feel right :)

any hints or help greatly appriciated.

regards,
Leo

Nov 17 '05 #2
Why catch the exception that you immediately throw? Let it bubble up.
Surround your "aTest.Active = true;" line with a try/catch. That way, the
code that sets the Active property will be notified when the error occurs,
which is the intended behavior.

Derrick

"Leif Eirik Olsen" <leif-eirik.olsen@_REMOVE_plugging.com> wrote in message
news:Od**************@TK2MSFTNGP15.phx.gbl...
Hi,

I have a class Test with a property Active. Setter and getter is
implemented.
Setter ~
set
{
try
{
if (SomeValue) throw new Exception("Error");
else FActive = value
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message)}
}

In my main application that uses this Test class I code something like
this:
Test aTest = new Test;
aTest.Active = true;
Do other stuff1
Do other stuff2
...

My problem: How do I break execution on the line "aTest.Active = true;" if
the setting of the Active property raised an exception ?
In other words: I do not want to execute "do other stuff1 & 2" if an
exception was raised on the previous line.

I know I can solve this by doing this:
Test aTest = new Test;
aTest.Active = true;
if (aTest.Active)
{
Do other stuff1
Do other stuff2
...
}
, but this does not feel right :)

any hints or help greatly appriciated.

regards,
Leo

Nov 17 '05 #3
"news.microsoft.com" <ra*************@gmail.com> skrev i melding
news:uD**************@TK2MSFTNGP15.phx.gbl...
In your main application that uses this Test class :

Test aTest = new Test;
try
{
aTest.Active = true;
}
catch
{
throw;
}
Do other stuff1
Do other stuff2


Thanks, but I do not think that will help much, as the "aTest.Active = true
Excpetion) is already handeled in the Setter, thus you suggested try -
catch -> throw will never fire. True?

regards,
Leo
Nov 17 '05 #4
"Derrick" <de***********@hotmail.com> skrev i melding
news:dk********************@news20.bellglobal.com. ..
Why catch the exception that you immediately throw? Let it bubble up.
Surround your "aTest.Active = true;" line with a try/catch. That way, the
code that sets the Active property will be notified when the error occurs,
which is the intended behavior.

Derrick


Yes, mabe your right, but then I end up with a lot of exception handeling in
my main application, instead of in my support-classes where I feel the
exception handeling belongs.
These things function a bit different in Delphi (pre .net where I've done
most of my work), mabe it is just another way of doing the same thing, and
I'll just need do get used to it :)

Thanks again,
Leo
Nov 17 '05 #5
Oh, sorry, missed that. I think the code below shall solve your problems,
also, you wont need to write exception handling messages everywhere.

Setter ~
set
{
try
{
if (SomeValue) throw new Exception("Error");
else FActive = value
}
catch (System.Exception ex)
{
throw;
}
}
Test aTest = new Test;
try
{
aTest.Active = true;
}
catch
{
throw //or messagebox.show is this is the end point;
}
Do other stuff1
Do other stuff2

--
http://codebetter.com/blogs/ranjan.sakalley/
"Leif Eirik Olsen" <leif-eirik.olsen@_REMOVE_plugging.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
"news.microsoft.com" <ra*************@gmail.com> skrev i melding
news:uD**************@TK2MSFTNGP15.phx.gbl...
In your main application that uses this Test class :

Test aTest = new Test;
try
{
aTest.Active = true;
}
catch
{
throw;
}
Do other stuff1
Do other stuff2


Thanks, but I do not think that will help much, as the "aTest.Active =
true
Excpetion) is already handeled in the Setter, thus you suggested try -
catch -> throw will never fire. True?

regards,
Leo

Nov 17 '05 #6
hi,

well that is the point of the exception after all, to inform to the caller
that the value used is not valid. , otherwise there is no need to throw an
exception and the code will look like this:

set
{

if (SomeValue)
MessageBox.Show(ex.Message);
else
else FActive = value
}
easier I think. I anyway see no point in that, why you do not set the value
of FActive to true somewhere?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Leif Eirik Olsen" <leif-eirik.olsen@_REMOVE_plugging.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...
"Derrick" <de***********@hotmail.com> skrev i melding
news:dk********************@news20.bellglobal.com. ..
Why catch the exception that you immediately throw? Let it bubble up.
Surround your "aTest.Active = true;" line with a try/catch. That way,
the
code that sets the Active property will be notified when the error
occurs,
which is the intended behavior.

Derrick


Yes, mabe your right, but then I end up with a lot of exception handeling
in
my main application, instead of in my support-classes where I feel the
exception handeling belongs.
These things function a bit different in Delphi (pre .net where I've done
most of my work), mabe it is just another way of doing the same thing, and
I'll just need do get used to it :)

Thanks again,
Leo

Nov 17 '05 #7
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> skrev
i melding news:OX**************@TK2MSFTNGP09.phx.gbl...
hi,

well that is the point of the exception after all, to inform to the caller that the value used is not valid. , otherwise there is no need to throw an
exception.....


Agreed.

Well, my problem is not a all 'show stopper', I'm just trying to understand
how to layout my code right.

The Test class with the property Active I'm talking about is in fact a
comport wrapper class, and the Active property opens/closes the selected
comport. In this case a bluetooth enabled comport. Not sure all agree with
me, but I would prefer to do the exception handeling at the 'property
setting level' in this case (if the comport fails to open, that is :)). If
I do this I get the undesired result that I, in my main application, have to
code like this:

aTest.Active = true;
if (aTest.Active) //do check because previous line could have raised an
exception and not set active to true
{
do other stuff...
}

I think what I'll do (don't laugh:))....I'll replace the Active property
with two methods OpenCom and CloseCom both returning a bool indicating
success or not. Then I can write my code (in main application) like this
(feels better):

if (aTest.OpenCom())
{
do other suff
}

regards,
Leo
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.