Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# multiple catch exceptions

// if C# v6+, you can use exception filters
catch (Exception ex) when (ex is FormatException || ex is OverflowException)
{
  // do something
}
Comment

c# catch multiple exception types

try
{
    /*  Try do something ... */
}
catch (Exception ex) when (ex is InvalidDataException || ex is ArgumentException)
{
    Console.WriteLine("a specific catch");
}
catch (Exception ex)
{
  Console.WriteLine("General catch");
}
Comment

c# catch two exceptions in one block

catch (Exception ex)            
{                
    if (ex is FormatException || ex is OverflowException)
    {
        WebId = Guid.Empty;
        return;
    }

    throw;
}
Comment

c# try catch multiple catches

            try
            {
                Console.WriteLine("Chose a number: ");
                int usrNo = Convert.ToInt32(Console.ReadLine());
                return usrNo;
            }
            catch (FormatException ex) 
            { 
              ErrorMessagePrintCustomMessage("You pressed a letter"); 
            }
            catch (Exception ex) { ErrorMessageErrorOccured(ex); }
Comment

c# catch multiple exceptions at once

catch (Exception ex)            
{                
    if (ex is FormatException or OverflowException) // Chain as many or xException as you need
    {
        WebId = Guid.Empty;
        return;
    }
    
    throw;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: get appsettings from app.config c# .net core 
Csharp :: C# api get value from header 
Csharp :: c# loop class properties add to array 
Csharp :: c# listview add item 
Csharp :: get connection string from web.config in c# 
Csharp :: c# how to compare 2 dates without time 
Csharp :: linq datatable 
Csharp :: Convert Json String to model Class or Object 
Csharp :: how to keep rigidbody2D upright unity 
Csharp :: c# const vs readonly 
Csharp :: vb.net check if datatable has rows 
Csharp :: listbox items to string c# 
Csharp :: delete all rows from table linq 
Csharp :: expando object c# 
Csharp :: search for a substring in the registry 
Csharp :: euler to quaternion 
Csharp :: unity audio source 
Csharp :: label wpf 
Csharp :: c# validate xml 
Csharp :: setting the parent of a transform which resides in a prefab 
Csharp :: listview inter thread operation not valid 
Csharp :: how to write text in specific position in c# 
Csharp :: int c = new int(); in C# 
Csharp :: conncet oracle database in c# visual studio 
Csharp :: c# program exit 
Csharp :: how to know pm or am C# 
Csharp :: cant see my classes in inspector 
Csharp :: regex only letters and numbers c# 
Csharp :: unity c# image invisible 
Csharp :: how to use monitor from system.threading in c# 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =