473,703 Members | 3,307 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 5189
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
2879
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
2338
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 catch the exception and keep going. I'd just get the error message and then it would shut the...
7
6003
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2749
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 please critique the following exception handling mechanism in terms of my requirements ?
2
2594
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 exception base class that will handle the errors and pass useful error messages to the user?
9
2538
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 much less efficient than returning values, or less efficient at all? Just curious...
44
4213
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, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
5133
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
3051
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 three handlers that all do the same thing. So, I could just catch Exception. But, is that...
1
3107
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
8749
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9243
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9002
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7853
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4420
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3113
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
2
2434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.