![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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. 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 Specs![]() |
![]() |
| 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 | |||