473,396 Members | 2,129 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,396 software developers and data experts.

Exception Handling in a Class or Worker Thread (.NET 2.0)

Hi

What is the correct (or best) way to handle (catch) exceptions in a class or
in a worker thread in .NET 2.0?
In my project, I've a class which do some cryptographic operations. This
class runs also in a seperate thread (not main thread). Now, how should I
handle the possible exceptions in this class?

Thanks and Regards,
Dominik

-----------------------------
http://blogs.ecreation.ch
Aug 20 '06 #1
5 1463
Hi Vadym

Thank you for your answer.

Now my next questions:
I use the try{} catch{} block in my class. Also, I catch the
CryptographicException. When a exception occurs in my class, should I throw a
new exception within the catch block? Should I raise a custom event? Or
should I write the exception message in a public property so that I can use
the message in the main thread?

I would like to know which is the best way (the Microsoft recommend way) to
handle exceptions and work with this exceptions.
Thanks and Regards,
Dominik

-----------------------------
http://blogs.ecreation.ch
"Vadym Stetsyak" wrote:
Hello, Zemp!

ZDWhat is the correct (or best) way to handle (catch) exceptions in a
ZDclass or in a worker thread in .NET 2.0?
ZDIn my project, I've a class which do some cryptographic operations.
ZDThis class runs also in a seperate thread (not main thread). Now, how
ZDshould I handle the possible exceptions in this class?

IMO it depends on the logic of your application.
Since, your thread is doing some security related stuff, exception there may be
treated as fatal error and application will exit with appropriate status
message in the logs ( EventLog, etc).

OTOH if you received exception and you're able to recover, then
catch(...) block can simply log exception in the EventLog or any other log.

As you can see there are various ways how to handle exception.
If you will provide more specific question with more context,
then the answer will be more concrete.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot
Aug 21 '06 #2
Hi Vadym
It depends on what you want to do with that exception. If you want to show it to
user then you can display it in the GUI using Control.Invoke technique (
multithreading under winforms ).
That means that I have to raise my custom event (when I would like to use
the Control.Invoke method)?

Regards,
Dominik

-----------------------------
http://blogs.ecreation.ch
"Vadym Stetsyak" wrote:
Hello, Zemp!

ZDNow my next questions:
ZDI use the try{} catch{} block in my class. Also, I catch the
ZDCryptographicException. When a exception occurs in my class, should I
ZDthrow a new exception within the catch block?

It depends on you, you can rethrow the same exception, you can create
new Exception object and throw it instead. But generally if you want
to notify the code down in callstack then you can simply rethrow an exception.

But generally, if your only rethrowing then catch has no sense...

ZDShould I raise a custom event? Or should I write the exception message
ZDin a public property so that I can use the message in the main thread?

It depends on what you want to do with that exception. If you want to show it to
user then you can display it in the GUI using Control.Invoke technique (
multithreading under winforms ). If you don't want to notify user about exceptions,
you can log and simply "swallow" that exception.

ZDI would like to know which is the best way (the Microsoft recommend
ZDway) to handle exceptions and work with this exceptions.

Take a look at ( http://msdn.microsoft.com/msdnmag/is...0/Reliability/ )
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot
Aug 21 '06 #3
Hello, Zemp!

ZDThat means that I have to raise my custom event (when I would like to
ZDuse the Control.Invoke method)?

Custom event will execute only if there are subscribers on it ( handlers ). And this
handler(s) will Control.Invoke. Or you can directly use Control.Invoke without custom event.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 22 '06 #4
Hi Vadym

You wrote: Or you can directly use Control.Invoke without custom event.

How can I do this? Do you have an example?

Regards,
Dominik

-----------------------------
http://blogs.ecreation.ch
"Vadym Stetsyak" wrote:
Hello, Zemp!

ZDThat means that I have to raise my custom event (when I would like to
ZDuse the Control.Invoke method)?

Custom event will execute only if there are subscribers on it ( handlers ). And this
handler(s) will Control.Invoke. Or you can directly use Control.Invoke without custom event.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot
Aug 22 '06 #5
Hello, Zemp!

Take a look at
( http://www.codeproject.com/csharp/winformthreading.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 23 '06 #6

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

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
28
by: dcrespo | last post by:
Hi all, How can I get a raised exception from other thread that is in an imported module? For example: --------------- programA.py ---------------
0
by: Mike Grishaber | last post by:
I am using .NET Remoting to connect my incoming HTTP Requests to my business logic. I am using a custom IHttpAsyncHandler to receive the incoming HTTPRequests and then I forward the...
2
by: Natalia DeBow | last post by:
Hi there, I am working on an client-server app, where the client asynchronously issues a request for the server to perform some action and the server is supposed to notify the client when the...
4
by: MadSage | last post by:
I currently have a multi-threaded server application with worker threads and a core thread. For all of these threads I have something like the following code: uint32 __stdcall...
2
by: Jordan | last post by:
I need to handle UI events in a worker thread instead of the primary UI thread. In C#, is the normal UI event handling behavior to run in a context thread on the thread pool or are events always...
2
by: Robinson | last post by:
Hi, I have 2 menu items on the Debug menu of my application: Throw Exception On Form and Throw Exception In Worker Thread The former just executes: throw new Exception ( "I'm testing...
1
by: [david] | last post by:
What am I doing wrong? I'm trying to capture stdErr in a multi-threaded program. This code crashes wxPython with /Py Assertion Error: C++ assertion "m_count=-1 || m_count=-2" failed/ What I'm...
1
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread. Logic is: 1) i will first create a thread to display a splash screen until a big process is completed 2)then i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.