473,654 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Howto Identify an Exception?

Hi,

For some procedures that throws exceptions, I would like to show different
messages to the user depending on what type of exception he's getting. For
instance this one: when the file is locked, I want a messagebox to tell that
the user has to close the file first.

Is there a way to identify an exception by some kind of unique number or
something like this? I don't want to identify it on the Exception Message
because some users are using French OS, and it'sj sutn ot right in my
opinion :-)

Thanks a lot in advance,

Pieter

Exemple of the exception:
Exception Source: mscorlib
Exception Type: System.IO.IOExc eption
Exception Message: The process cannot access the file 'C:\Documents and
Settings\LBN\De sktop\Proforma. xls' because it is being used by another
process.
Exception Target Site: WinIOError

---- Stack Trace ----
System.IO.__Err or.WinIOError(e rrorCode As Int32, maybeFullPath As String)
Ghost.exe: N 2015149
System.IO.FileS tream.Init(path As String, mode As FileMode, access As
FileAccess, rights As Int32, useRights As Boolean, share As FileShare,
bufferSize As Int32, options As FileOptions, secAttrs As
SECURITY_ATTRIB UTES, msgPath As String, bFromProxy As Boolean)
Ghost.exe: N 00998
System.IO.FileS tream..ctor(pat h As String, mode As FileMode, access As
FileAccess)
Ghost.exe: N 00057
System.Windows. Forms.SaveFileD ialog.OpenFile( )
Ghost.exe: N 00154
Microsoft.Repor ting.WinForms.E xportDialog.Pro mptFileName(fil eExtension As
String)
Ghost.exe: N 00369
Microsoft.Repor ting.WinForms.E xportDialog.OnE xportCompleteUI (result As
ProcessThreadRe sult, data As Object)
Ghost.exe: N 00116
Nov 16 '06 #1
6 2489
Why not use the standard try catch blocks?

i.e.

Try
' Whatever
Catch ex as InvalidOperatat ionException
msgbox(" Invalid Op!")
Catch ex as FileIOException
msgbox("File IO Exception!")
Catch ex as Exception
' Grabs all the other exception (well, for the most part)
msgbox(ex.messa ge)
End Try

Thanks,

Seth Rowe

Pieter wrote:
Hi,

For some procedures that throws exceptions, I would like to show different
messages to the user depending on what type of exception he's getting. For
instance this one: when the file is locked, I want a messagebox to tell that
the user has to close the file first.

Is there a way to identify an exception by some kind of unique number or
something like this? I don't want to identify it on the Exception Message
because some users are using French OS, and it'sj sutn ot right in my
opinion :-)

Thanks a lot in advance,

Pieter

Exemple of the exception:
Exception Source: mscorlib
Exception Type: System.IO.IOExc eption
Exception Message: The process cannot access the file 'C:\Documents and
Settings\LBN\De sktop\Proforma. xls' because it is being used by another
process.
Exception Target Site: WinIOError

---- Stack Trace ----
System.IO.__Err or.WinIOError(e rrorCode As Int32, maybeFullPath As String)
Ghost.exe: N 2015149
System.IO.FileS tream.Init(path As String, mode As FileMode, access As
FileAccess, rights As Int32, useRights As Boolean, share As FileShare,
bufferSize As Int32, options As FileOptions, secAttrs As
SECURITY_ATTRIB UTES, msgPath As String, bFromProxy As Boolean)
Ghost.exe: N 00998
System.IO.FileS tream..ctor(pat h As String, mode As FileMode, access As
FileAccess)
Ghost.exe: N 00057
System.Windows. Forms.SaveFileD ialog.OpenFile( )
Ghost.exe: N 00154
Microsoft.Repor ting.WinForms.E xportDialog.Pro mptFileName(fil eExtension As
String)
Ghost.exe: N 00369
Microsoft.Repor ting.WinForms.E xportDialog.OnE xportCompleteUI (result As
ProcessThreadRe sult, data As Object)
Ghost.exe: N 00116
Nov 16 '06 #2
Pieter wrote:
Is there a way to identify an exception by some kind of unique number or
something like this?
I may be wrong but I don't believe there's a way to do this. You
could try calling the Win32 method GetLastError() but I don't think
there's any guarantee that it wasn't reset to something else
between the file access and when you got the exception.

This is a nice concept but personally I've never found this type of
error handling that useful plus it can be time consuming and difficult
to keep up with all of the lower level error codes that may come back.
In addition it may be so far removed from your original request that
it's impossible disambiguate between multiple scenarios. What if
some virus scanner is holding on to a lock to that file and when the
user tries to open it your message tells them to close it. Then
they'll just get annoyed with your app because they don't have it
open. That later will just get them annoyed with windows in general
- which is always better.
Personally I feel that higher level processes should provide more
general type errors. For example just a "Failed to open file" with
the option to look at the details which would be "The process cannot
access the file 'C:\Documents and
Settings\LBN\De sktop\Proforma. xls' because it is being used by another
process."

Nov 16 '06 #3

rowe_newsgroups wrote:
Why not use the standard try catch blocks?
I thought that he was trying to distinguish between multliple
exceptions that may all be of the type IOException since there aren't
exception classes for every type of error.

Nov 16 '06 #4
I want indeed try to distinguish, but it could alreaddy be a start.
Thansk guys!

"Israel" <is**********@h otmail.comwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
>
rowe_newsgroups wrote:
>Why not use the standard try catch blocks?

I thought that he was trying to distinguish between multliple
exceptions that may all be of the type IOException since there aren't
exception classes for every type of error.

Nov 16 '06 #5

"Pieter" <pi************ ****@hotmail.co mwrote in message
news:eU******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

For some procedures that throws exceptions, I would like to show different
messages to the user depending on what type of exception he's getting. For
instance this one: when the file is locked, I want a messagebox to tell
that the user has to close the file first.

Is there a way to identify an exception by some kind of unique number or
something like this? I don't want to identify it on the Exception Message
because some users are using French OS, and it'sj sutn ot right in my
opinion :-)

Thanks a lot in advance,

Pieter
You should handle exceptions you know about explicitly. If you expect you
might get a general error (i.e. an error you don't know about or didn't
think about), then you catch it with a standard "catch all". It is often
useful to put the catch all exception handler as a top level handler in your
main method, especially if you intend to terminate the program when it
happens:

Now as for your French users, in general users won't have a clue what the
exception message returned means, no clue whatsoever, even the English
speaking ones (most of them actually) especially in the context of your
running program, so there is no need to provide translations of those
messages at all. What you DO need to do, is to provide some kind of message
box/logging facility so the user can at least copy the message to the
clip-board to send to your support.

In the general case, my software operates with different error levels, the
highest is "Exception" (SystemFatal), given that if I don't know what the
error is, I have no confidence that the software can continue without
borking up something else. For example:

Public Enum ErrorClassEnum

OK ' Success
Fail 'In a multiple iteration operation, fail the iteration, not the
whole operation
Fatal 'Terminate the entire operation
SystemFatal 'Terminate the program
Cancel 'Terminate the entire operation, don't display an error message

End Enum

the "Ex As Exception" is SystemFatal for me, whereas the SqlException might
be SystemFatal, Fatal or Fail, depending on what the error number is. Many
SystemFatal exceptions are caused by bad programming rather than real system
errors. For example, divison by zero, null-reference exception, etc. You
can code defensively to avoid many of these ever happening.



Nov 16 '06 #6
Pieter,

The trick is to put several individual try/catch blocks around small
blocks of code. If you need that finely-grained control, a "block" may
be a single method call. RTFM and decide what you need to catch.

For instance, on the StreamReader(st ring) constructor, there are 5
possible exceptions that can be thrown. Based on context, each one can
mean only one thing. Do something like:

// Get string strFilename from user.
StreamReader myStream;
try
{
myStream = new StreamReader(st rFilename);
}
catch (ArgumentExcept ion)
{
// The filename string was empty.
}
catch (ArgumentNullEx ception)
{
// The filename string was null.
}
catch (FileNotFoundEx ception)
{
// At this point, we're sure that the filename string was valid, but
there's
// no valid file at the location it points to on the filesystem.
}
catch (DirectoryNotFo undException)
{
// At this point, we're sure that the filename string was valid, but
part
// of the path that it refers to is invalid.
}
catch (IOException)
{
// At this point, we know that the format of the filename is
incorrect.
}

Similarly, just about every operation involving any I/O has a similar
canonical exception list. See the documentation and figure out exactly
what to catch and what it means for the particular functions you need.

I believe it's bad coding style to *need* to catch all of those
exceptions in most cases. A much better way to do this is to
pre-validate (contract check) every variable. For instance, the
following code:

if (strFilename == null || strFilename == String.Empty)
// Break out: the filename is invalid

will take care of the first two exceptions. File.Exists() will tell you
if the file exists or not and save you catching the other three
exceptions. No exceptions are thrown -- throwing an exception is a
highly costly process -- and you get the same information. It also has
the added benefit of saving the exceptions for real exception cases.
For instance, if the user specifies a file, then between when you
capture the filename and when you try to do something with the file,
they unplug the USB device that the file is on, that's a real
exception. It's probably one you can't handle at the level of this
method, so it should get bubbled up to the main thread and handled
there, probably by telling the user, "you've done something I didn't
expect and can't correct."
Stephan

Pieter wrote:
Hi,

For some procedures that throws exceptions, I would like to show different
messages to the user depending on what type of exception he's getting. For
instance this one: when the file is locked, I want a messagebox to tell that
the user has to close the file first.

Is there a way to identify an exception by some kind of unique number or
something like this? I don't want to identify it on the Exception Message
because some users are using French OS, and it'sj sutn ot right in my
opinion :-)

Thanks a lot in advance,

Pieter

Exemple of the exception:
Exception Source: mscorlib
Exception Type: System.IO.IOExc eption
Exception Message: The process cannot access the file 'C:\Documents and
Settings\LBN\De sktop\Proforma. xls' because it is being used by another
process.
Exception Target Site: WinIOError

---- Stack Trace ----
System.IO.__Err or.WinIOError(e rrorCode As Int32, maybeFullPath As String)
Ghost.exe: N 2015149
System.IO.FileS tream.Init(path As String, mode As FileMode, access As
FileAccess, rights As Int32, useRights As Boolean, share As FileShare,
bufferSize As Int32, options As FileOptions, secAttrs As
SECURITY_ATTRIB UTES, msgPath As String, bFromProxy As Boolean)
Ghost.exe: N 00998
System.IO.FileS tream..ctor(pat h As String, mode As FileMode, access As
FileAccess)
Ghost.exe: N 00057
System.Windows. Forms.SaveFileD ialog.OpenFile( )
Ghost.exe: N 00154
Microsoft.Repor ting.WinForms.E xportDialog.Pro mptFileName(fil eExtension As
String)
Ghost.exe: N 00369
Microsoft.Repor ting.WinForms.E xportDialog.OnE xportCompleteUI (result As
ProcessThreadRe sult, data As Object)
Ghost.exe: N 00116
Nov 16 '06 #7

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

Similar topics

8
487
by: DraguVaso | last post by:
Hi, I want my application do different actions depending on the exception it gets. For exemple: I have an SQL-table with a unique index. In case I try to Insert a record that's alreaddy in it I get this exception: "Cannot insert duplicate key row in object 'tblTelephones' with unique index 'UniqueValues'." What I'm looking for is a way to identify the exception: in case I get this
11
2281
by: Jarek | last post by:
Hi all! Is it possible to identify throw statement, within catch( ... )? Can I identify type of exception passed to catch(...) ? I'm trying to identify problem, which is probably somewhere in library which I'm using. This is real-time program, so step-by-step debbuging is very diffcult. Program is written in gcc-3.3 working on Linux best regards
6
1223
by: Pieter | last post by:
Hi, For some procedures that throws exceptions, I would like to show different messages to the user depending on what type of exception he's getting. For instance this one: when the file is locked, I want a messagebox to tell that the user has to close the file first. Is there a way to identify an exception by some kind of unique number or something like this? I don't want to identify it on the Exception Message because some users are...
0
8376
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
8290
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8708
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8594
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
7307
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...
0
4149
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...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
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
1916
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.