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 7 5148
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
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
"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
"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
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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
by: Noor |
last post by:
please tell the technique of centralize exception handling without try catch
blocks in c#.
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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{
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |