0

Is there any reason to use specific expetion classes MyException1 and MyException2 in this case?

public static void Main() { try { TestMethod(); } catch(Exception ex) { Console.Writeline(ex); } } private static void TestMethod() { // This method can throw Exception1 and Exception2 } public class MyException1 : Exception {} public class MyException2 : Exception {} 

I know that it makes sense in case when we have several catch blocks for each exception type. But in this case MyException1 and MyException2 are similar empty. These throwed exceptions will be casted to Exception class in the Main method. Maybe is it better not to create two similar Exception classes with such handling?

7
  • That is the whole reason that Exceptions can be handled from specific to least specific so you do not have to write a handler for every exception type that could be thrown. It allows you to do just what you are proposing, have a general handler for anything where you do not want to have / write specific handling logic. Commented Nov 6, 2018 at 17:31
  • We tend to mostly just use the Exception class directly, and only implement/catch more specific exceptions when we plan to actually handle some specific scenario differently. Your needs may differ, but it's worked fairly well for us in practice. Commented Nov 6, 2018 at 17:36
  • @mason Catching Exception is generally a bad idea. It's useful when logging, but you should (nearly) always throw again. What would you do if you caught an OutOfMemoryException? Commented Nov 6, 2018 at 18:00
  • "I know that it makes sense in case when we have several catch blocks for each exception type. But in this case MyException1 and MyException2 are similar empty." You seem to be conflating empty catch block with exception class with an empty implementation. These are totally different concepts. Can you please clarify? Commented Nov 6, 2018 at 18:01
  • @Neil No, it's not a bad idea. We haven't seen a lot of OOM exceptions. My point being that in most cases when you're handling an exception, your code doesn't really care what the exception was. The point is your code failed to perform and you need to gracefully handle it. Now in some cases, you might want to catch specific exceptions if you know ahead of time that it's likely to be thrown somewhere, but you'd only want to do so if you're going to handle it different from any other other error. If you add many custom exceptions but handle them all the same, you've added needless complexity. Commented Nov 6, 2018 at 18:05

3 Answers 3

2

The concept behind a catch block is that you handle the exception. If a certain type of exception requires a certain type of handling, it is helpful when that exception has its own class, so it can have its own catch block.

For example, if MyException1 can be safely swallowed while MyException2 is fatal, you could write:

 try { DoSomethingHard(); } catch (MyException1 exception1) { _log.Write("Warning: small exception, no worries. {0}", exception1.Message); continue; } catch (MyException2 exception2) { _log.Write("Fatal: big exception, gotta bail out now. {0}", exception2.Message); break; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I understand it, but the question was about rationality of using specific exceptions. Does it make sense use two empty specific exceptions with self descriptive names with the following handling in catch(Exception ex) block? For example, this common catch block contains code for further throwing an exception.
It's OK for the caller not to have specific catch blocks for each possible exception type, regardless of whether those exception types have exception-specific properties. Is that what you are asking?
0

Exceptions should be wide rather than deep. Have a different exception for each, erm... exception.

Your example doesn't really show a good example. Perhaps if it was more like:

public static void Main() { try { TestMethod(); } catch(Exception ex) { Console.Writeline(ex); } } private static void TestMethod() { if(..bad configuration) throw new ConfigurationException("configuration item"); if(missing file) throw new FileMissingException("filename"); // This method can throw Exception1 and Exception2 } public class ConfigurationException : Exception {} public class FileMissingException : Exception {} 

2 Comments

Yes, this is better example. But shoud I use specific catch block for each type of exception if I want to throw the exception further?
Only catch what you can handle. If you think you need to do something with missing files but not configuration exceptions, then only handle the FileMissingException. Configuration errors are not really something you can handle, so it's probably best to let the application crash in that case, and the developer could fix the configuration settings.
0

If you are using ASP.NET, its so common that you create and use your own Exception handler, in that case, you can to consider specific behavior for each exception in only one method (not catch block in every error prone code blocks), look at this simple example:

public class MyExceptionHandler : ExceptionHandler { public override void Handle(ExceptionHandlerContext context) { if (context.Exception is SqlException) { //do something... } else if (context.Exception is HttpListenerException) { //do something... } else { //do something else... } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.