473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Erro r");
else FActive = value
}
catch (System.Excepti on 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.Acti ve = 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 5170
Setter ~
set
{
try
{
if (SomeValue) throw new Exception("Erro r");
else FActive = value
}
catch (System.Excepti on 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@_RE MOVE_plugging.c om> wrote in message
news:Od******** ******@TK2MSFTN GP15.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("Erro r");
else FActive = value
}
catch (System.Excepti on 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.Acti ve = 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.Acti ve = 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@_RE MOVE_plugging.c om> wrote in message
news:Od******** ******@TK2MSFTN GP15.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("Erro r");
else FActive = value
}
catch (System.Excepti on 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.Acti ve = 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******** ******@TK2MSFTN GP15.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.Acti ve = 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******** ************@ne ws20.bellglobal .com...
Why catch the exception that you immediately throw? Let it bubble up.
Surround your "aTest.Acti ve = 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("Erro r");
else FActive = value
}
catch (System.Excepti on 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@_RE MOVE_plugging.c om> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
"news.microsoft .com" <ra************ *@gmail.com> skrev i melding
news:uD******** ******@TK2MSFTN GP15.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.Acti ve =
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@_RE MOVE_plugging.c om> wrote in message
news:e9******** ******@TK2MSFTN GP09.phx.gbl...
"Derrick" <de***********@ hotmail.com> skrev i melding
news:dk******** ************@ne ws20.bellglobal .com...
Why catch the exception that you immediately throw? Let it bubble up.
Surround your "aTest.Acti ve = 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.mach in AT dot.state.fl.us > skrev
i melding news:OX******** ******@TK2MSFTN GP09.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'l l 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
2848
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
2328
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 assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't...
7
5976
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2740
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 exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you...
2
2586
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 Layer. My questions is where should I put exception handling: 1) Should it be put in all significant methods in all layers? 2) Should I create an...
9
2529
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be...
44
4179
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box,...
4
5117
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 .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
41
3033
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 handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out...
1
3094
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
7698
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.