How to catch multiple exception types in a single catch block in C#?
Hello everyone! I'm a beginner C# developer, and I have a problem with exception handling. I have a method that performs file and database operations. I want to handle different exception types (e.g., `FileNotFoundException`, `IOException`, `SqlException`, and `ArgumentException`) in the same way — log the error and show a message to the user. Currently my code looks like this:
```csharp
try
{
// some code that may throw different exceptions
}
catch (FileNotFoundException ex)
{
LogError(ex);
ShowMessage("File not found");
}
catch (IOException ex)
{
LogError(ex);
ShowMessage("I/O error");
}
catch (SqlException ex)
{
LogError(ex);
ShowMessage("Database error");
}
catch (ArgumentException ex)
{
LogEr
No answers yet.
Log in to leave an answer
Blogs
Optimizing Java Application Performance: A Practical Guide
Monorepo with JavaScript: Organizing Code in Practice
How to place the cursor at the end of text in EditText: Kotlin for Beginners
How to check if an array is associative or not in PHP: top mistakes