Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# catch multiple 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 :: how to initialize array in c# 
Csharp :: unity convert pixels to units 
Csharp :: get all the file from directory except txt in c# 
Csharp :: DrawImage resize to target size c# 
Csharp :: wpf user parent controller datacontext 
Csharp :: c# interoperability with linux or bash script 
Csharp :: C# if...else Statement 
Csharp :: Find Number of Repetitions of Substring 
Csharp :: c# get hwid 
Csharp :: using mediamanager how to play mp3 files 
Csharp :: permutation and combination program in c# 
Csharp :: ascx access parent master page 
Csharp :: virtual properties and lazy loading in c# 
Csharp :: open aspx page c# 
Csharp :: Wait some seconds without blocking UI execution 
Csharp :: nunjucks if variable exists 
Csharp :: can a dictionary type use get set c# 
Csharp :: id dublication exception c# .net core 
Csharp :: populate toolstripitems to combobox 
Csharp :: blender how to switch cameras 
Csharp :: Hangfire Creation Table With EFCore 
Csharp :: c# please build the project and retry 
Csharp :: Program to find GCD or HCF of two numbers c# 
Csharp :: .netstandard distinctby iqueryable 
Csharp :: death transition unity 2d 
Csharp :: c# dictionary contain key but returns false 
Csharp :: unity rotatoin angle 
Csharp :: wpf open new Window in MVVM 
Csharp :: attributes C# reflection variable update site:stackoverflow.com 
Csharp :: convert array to datatable c# 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =