-1

I catch Exception.

I would like to do something if that is a specific error. I can easily identify it with the string.

However I wondered if there is a cleaner way to do this. For example, when I create my own wrapper for exception I add an index.

Is there such a thing in default exception?

The function I use that throws errors is an instantiation of RemoteWebDriver (selenium object). However I don't need an answer specific for that class.

To be more precise, I catch the following error: OpenQA.Selenium.WebDriverException. However it may be thrown if there is a timeout when creating a driver or if a js file is closed. They have the save HResult.

So I see no way to differentiate them other than to check string unfortunately...

Cher
  • 2,789
  • 10
  • 37
  • 64
  • Do you want to catch a specific subclass of `Exception`? Or one with a specific message? – Blorgbeard Jul 13 '16 at 21:28
  • Well, pretty vague. First of all do not catch Exception but prefer more derived classes. Some of them have a numeric error code to discriminate more specific errors. Base class has HResult property too. Without more context... – Adriano Repetti Jul 13 '16 at 21:29
  • You can `catch (SomeSubClassOfException)`, or in C# 6, you can use [exception filters](http://stackoverflow.com/questions/4268223/c-sharp-exception-filter). – Blorgbeard Jul 13 '16 at 21:30
  • I'd like one with a specific message. About the subclass I use this, but more than 1 message is associated with the same subclass... so I still need to check the substring. HResult property I'll look into that thanks – Cher Jul 13 '16 at 21:32
  • 1
    @CherrysaHerrim Filtering exceptions by their message is a big design flaw in your code!! – Matías Fidemraizer Jul 13 '16 at 21:35
  • @MatíasFidemraizer That is exactly the reason of my question, to correct this !! :P – Cher Jul 13 '16 at 21:37
  • @CherrysaHerrim Sadly if who implemented that exception thought that two completely different issues should be thrown with the same exception, you're stuck in implementing a workaround like yours....... – Matías Fidemraizer Jul 13 '16 at 21:39
  • If you have existing code you want to change, you need to [edit](http://stackoverflow.com/posts/38361928/edit) your post to include the code. – Dour High Arch Jul 13 '16 at 21:40

1 Answers1

3

You can catch specific exceptions. For example...

try
{
    //Do things with your RemoteWebDriver...
}
catch (ImportantException e)
{
    //Do something important with this specific exception
}
catch (Exception e)
{
    throw;
}

This code "does" something specific when and only when an Exception of type ImportantException gets thrown, but in all other cases, it does something else (in this example, it just throws the exception).

You can also use exception filters as of C#6. See: https://stackoverflow.com/a/4268291/1672990

Community
  • 1
  • 1
Eric Sondergard
  • 565
  • 5
  • 22
  • thanks, however I like to filter exactly one exception without having to filter the string, not sure how to do it? – Cher Jul 13 '16 at 21:36
  • @CherrysaHerrim I am not sure what "string" you are referring to. – Eric Sondergard Jul 13 '16 at 21:39
  • I have at least 3 errors of the same type, but the contained message is different. As of now, this is the only way I foundto differentiate them. – Cher Jul 13 '16 at 21:47
  • @CherrysaHerrim - Do you have control over the creation of the exceptions such that you can edit the message and other properties of it? – keyboardP Jul 13 '16 at 21:48
  • @keyboardP Unfortunately I use dll (selenium web driver) – Cher Jul 13 '16 at 23:09
  • @CherrysaHerrim In that case, I would use the same method as above, but once you have caught the exception, examine the contents of its messages and react as needed. It really needs to be stressed that changing the behavior of your program based on exception message contents in this way is really not desirable. If a different message that you've never seen before comes up, or you update your libraries, you may have to make changes to accommodate these updates. It's usually not desirable to determine behaviors based on the contents of a string in general, as they are liable to change. – Eric Sondergard Jul 15 '16 at 21:05