Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Passing exception to a common sub

Reply
 
Old 04-26-2009   #1 (permalink)
Young


 
 

Passing exception to a common sub

Instead of handling exception in each function/sub, I would like to pass the
exception to a sub and get it to handle all exceptions. Here's where I try
to do:

Try
.....

Catch Ex as Exception

Call HandleEx(Ex) ' HandleEx is the sub

end try

Here's HandleEx:

Sub HandleEx(Ex as Exception)
dim msg as string
msg = Ex.Message
Msgbox (Msg) ' This works.
end sub

How can I get the exception type within HandleEx? What I am trying to do is
this:

Sub HandleEx(Ex as Exception)

If TypeOf Ex Is System.ArgumentException Then

ElseIf TypeOf Ex Is System.IO.FileNotFoundException then

Endif

end sub

The above works but I can I do it in a Select Case statement instead of use
If TypeOf Ex Is?

Please Help.
Thanks
Young




My System SpecsSystem Spec
Old 04-27-2009   #2 (permalink)
Cor Ligthert[MVP]


 
 

Re: Passing exception to a common sub

Young,

I agree for the most part of Pavel his message, you should not want this

But you can forever pass something as object.

However, in general it is bad practice to use object as a type and so it is
here.

Cor
Quote:

>
My System SpecsSystem Spec
Old 04-27-2009   #3 (permalink)
HEX


 
 

Re: Passing exception to a common sub

Hi

Before main Form run
you can setup general catch for all your exceptions which will not be
handled in your code

static void Main()
{
CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.OnThreadException);
}

internal class CustomExceptionHandler
{

// Handles the exception event.
public void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
result = this.ShowThreadExceptionDialog(t.Exception);
}
catch
{
try
{
MessageBox.Show("Fatal Error.", "Fatal Error",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}

// Exits the program when the user clicks Abort.
if (result == DialogResult.Abort)
Application.Exit();
}

// Creates the error message and displays it.
private DialogResult ShowThreadExceptionDialog(Exception e)
{
string errorMsg = "An error occurred please contact the
adminstrator.\n\n";
errorMsg = errorMsg + e.Message;
//Data.op.log.WriteEntry(e.ToString(),
System.Diagnostics.EventLogEntryType.Error);
return MessageBox.Show(errorMsg, "Application Error",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
}

"Young" <young10000@xxxxxx> wrote in message
news:49f51fd9$1@xxxxxx
Quote:

> Instead of handling exception in each function/sub, I would like to pass
> the exception to a sub and get it to handle all exceptions. Here's where I
> try to do:
>
> Try
> ....
>
> Catch Ex as Exception
>
> Call HandleEx(Ex) ' HandleEx is the sub
>
> end try
>
> Here's HandleEx:
>
> Sub HandleEx(Ex as Exception)
> dim msg as string
> msg = Ex.Message
> Msgbox (Msg) ' This works.
> end sub
>
> How can I get the exception type within HandleEx? What I am trying to do
> is this:
>
> Sub HandleEx(Ex as Exception)
>
> If TypeOf Ex Is System.ArgumentException Then
>
> ElseIf TypeOf Ex Is System.IO.FileNotFoundException then
>
> Endif
>
> end sub
>
> The above works but I can I do it in a Select Case statement instead of
> use If TypeOf Ex Is?
>
> Please Help.
> Thanks
> Young
>
>
>
My System SpecsSystem Spec
Old 04-27-2009   #4 (permalink)
Phill W.


 
 

Re: Passing exception to a common sub

Young wrote:
Quote:

> Instead of handling exception in each function/sub, I would like to pass the
> exception to a sub and get it to handle all exceptions.
IMHO, this is a Bad Idea.

The important part about Exception Handling is the "Handling" part, i.e.
doing something /useful/ about an Exception in the /context/ in which it
happens.

The same exception can be raised in many different places; take the
FileNotFoundException, for example. You could get this when:
(a) You try to read a user's "options" file for the very first time; no
big deal, you'll create it later, so you can safely catch (and choose to
ignore) this Exception.
(b) You try to open the "licence" file (that your installer created)
that states who is allowed to run your program. If that's missing, you
have a big problem and the program should stop dead.

Most importantly, you have to deal with (i.e. "handle") both of the
above cases (same Type of Exception, remember) in /completely/ different
ways.

Always try to keep your handling code as close to that which throws the
Exception(s) as you can, preferably within the same method.


That said, you can create an Application-level, catch-all Exception
Handler but (AFAIK) this will only be called just before your program
crashes and burns and the Run-Time tears it down and throws it away
(it's one stop short of the "a program has encountered a problem"
dialog, but only one).

Regards,
Phill W.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Just passing along some information Vista mail
Exception stack is corrupt when catching and storing the exception .NET General
Vista Install Error: Exception Unknown Software Exception Vista installation & setup
Passing Validation Vista General
passing arguments PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46