1
  1. Why we need multiple "catch" blocks even though we can write one generic exception?

  2. Is that important to know all the exception types and their purposes to make a good piece of code?

    I googled a lot but still have confusions in exception handling. Any good example?

Generic Exception:

try{
//some code
}
catch(Exception e){
//print e
}
//that's it.

Multiple catches

try{
//some code
}
catch(IOException e){
}
catch(SQLException e){
}

2 Answers 2

1

There are several advantages of using multiple exceptions:

  1. General exceptions will not let you know the exact root cause of the issue especially if many steps/checks involved in a method implementation. Also, If the exception occurs due to various reasons, you need to throw the different types of exceptions from your caller method implementation.

Eg: You can throw custom exceptions.

Here is your service code:

    public void Login(string username, string password)
    {
      if(string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
      {
      throw InvalidUserNameException();
      }
      if(!IsInternetAvaialable()) 
      {
      throw NoInternetAvailableException()
      }
      else
      {
      //Implement though your login process and if need use various custom exceptions or throw the exception if occurs.
      }
    }

    public class InvalidUserNameException : Exception
    { 
      public InvalidUserNameException()
      {
      }

      public InvalidUserNameException(string message)
        : base(message)
      {
      }

      public InvalidUserNameException(string message, Exception inner)
        : base(message, inner)
      {
      }
   }

Caller Method:

try {
  ...
} catch(InvalidUserNameException e) {
  // Show Alert Message here
} catch(NoInternetAvaibleException e) {
  // Show Alert Message with specific reason
}
catch(Exception e) {
  // Other Generic Exception goes here
}

Reference: https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm, this makes sense. Thank you!
0

1. Why we need multiple "catch" blocks even though we can write one generic exception?

Sometimes you might need to specify what causes the problem.

For example,

try {
  ...
} catch(IOException e) {
  // Print "Error: we cannot open your file"
} catch(SQLException e) {
  // Print: "Error: we cannot connect to the database"
}

With different errors, users can understand what went wrong easily.

If we go with

try {
  ...
} catch(Exception e) {
  // Print "Error: " + e.
}

It's harder for the users to figure out what went wrong.

Also, we can send the users to different pages accordingly to the error if we use multiple catch-es.


2.Is that important to know all the exception types and their purposes to make a good piece of code?

Personally, I would go with important exceptions such as IO, DB, etc. that can cause serious trouble. For others, I would catch with general exception.

1 Comment

It really depends on the code. I normally go with the exceptions stated in the docs and relate to my program. Those should be enough.