Error Handler...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    Error Handler...

    So I'm making a method to get the Error Type, and display a message accordingly based on that...

    Code:
    switch (exception) {
    case FileNotFoundException:
    // do stuff...
    break;
    Error: FileNotFoundExc eption is a 'type' used like a 'variable'
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    There is no actual question in your post

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Hmm, where to start. Ok, the first thing I'll say is you can't do what you're trying to do... I'll carry on with explaining why but sorry if it seems a bit disjointed.

      To address your immediate question, you are getting that error message because you're using a type like a variable, just like the error message says. A switch statement is expecting a value, not a type. If you want that to work, you'd need to change it to...

      Code:
      switch (e.GetType())
      {
          case typeof(FileNotFoundException):
              // do stuff
              break;
      }
      Now, that still won't compile because a switch statement can only work on constant values of an integral type (things like int, byte, etc... strings count here too). So what you'd need to do is change that switch statement to an if-else-if-else statement, as such...

      Code:
      if (e.GetType() == typeof(FileNotFoundException))
      {
          // do stuff
      }
      else if (e.GetType() == <exception type>)
      {
          // do stuff
      }
      else
      {
          // do stuff
      }
      Now, you can do this right as a part of your try/catch block, so you don't really need to set this up yourself, you just catch the exception you want and handle it accordingly. The caveat here is that you have to organize them properly, you catch the specific exceptions first, then the generic exceptions. So, for example...

      Code:
      try
      {
      
      }
      catch (FileNotFoundException e)
      {
          // do stuff
      }
      catch (IndexOutOfRangeException e)
      {
          // do stuff
      }
      catch (Exception e)
      {
          // do stuff
      }
      You have to catch Exception last because it's a more general than the other two. This is actually based of inheritance. If you have Visual Studio, you can right-click on the exception type and select "Go To Definition" to bring up the metadata and see the class definition and follow the inheritance chain.

      I hope that helps :)

      Comment

      • Samishii23
        New Member
        • Sep 2009
        • 246

        #4
        There is no actual question in your post
        What am I doing wrong?

        If you have Visual Studio, you can right-click on the exception type and select "Go To Definition" to bring up the metadata and see the class definition and follow the inheritance chain.
        The meta data for all the exceptions that can happen with the given input?

        I was going to do the try-catch blocks at the processing point, and have a function called to log, and throw a message box to the user stating the issue, so maybe they can fix it on their end. Also in case of certain errors, I would have the function close the application.

        Searching through the VS data.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          The meta data for all the exceptions that can happen with the given input?
          No, what I meant by that is if you want to see the inheritance path for an exception, you can follow it doing that.

          You should be able to use the last method that I had there and do what you want. If there's anything you want to do at the end of your try/catch bock, regardless of the error, you can use finally. I'd recommend not closing your form right in the catch part though, perhaps set a flag so that it can close properly at a more appropriate time.

          Comment

          Working...