Uso esteso dell’istruzione throw [c#]

La parola chiave throw viene utilizzata in C# per sollevare un’eccezione manualmente utilizzando la parola chiave throw.

Qualsiasi tipo di eccezione derivato dalla classe Exception può essere generato utilizzando la parola chiave throw.

Il suo utilizzo è semplice :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static void Main(string[] args)
{
Persona pers= null;
try
{
PrintPersona(pers);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message );
}
Console.ReadKey();
}
private static void PrintPersona( Persona pers)
{
if (pers == null)
throw new NullReferenceException("Oggetto persona null.");
Console.WriteLine(pers.name);
}
static void Main(string[] args) { Persona pers= null; try { PrintPersona(pers); } catch(Exception ex) { Console.WriteLine(ex.Message ); } Console.ReadKey(); } private static void PrintPersona( Persona pers) { if (pers == null) throw new NullReferenceException("Oggetto persona null."); Console.WriteLine(pers.name); }
static void Main(string[] args)
{
    Persona pers= null;

    try
    {
        PrintPersona(pers);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message );
    }                      

    Console.ReadKey();
}

private static void PrintPersona( Persona pers)
{
    if (pers  == null)
        throw new NullReferenceException("Oggetto persona null.");
        
    Console.WriteLine(pers.name);
}

 

Fino a C # 6.0 “throw” poteva essere utilizzata solo come istruzione autonoma, ovvero non può essere parte di un’espressione.

C # 7.0 supera questa limitazione e quindi consente a questa parola chiave di essere posizionata ovunque, anche ad esempio nel mezzo di un operatore ternario.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static void Main(string[] args)
{
Student std = null;
try
{
PrintStudentName(std);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message );
}
Console.ReadKey();
}
private static void PrintStudentName( Student std)
{
string result= (std == null) ? throw new NullReferenceException("Student object is null.") : std.StudentName;
Console.WriteLine(result);
}
static void Main(string[] args) { Student std = null; try { PrintStudentName(std); } catch(Exception ex) { Console.WriteLine(ex.Message ); } Console.ReadKey(); } private static void PrintStudentName( Student std) { string result= (std == null) ? throw new NullReferenceException("Student object is null.") : std.StudentName; Console.WriteLine(result); }
static void Main(string[] args)
{
    Student std = null;

    try
    {
        PrintStudentName(std);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message );
    }                      

    Console.ReadKey();
}

private static void PrintStudentName( Student std)
{
  string result= (std  == null) ?  throw new NullReferenceException("Student object is null.") :  std.StudentName;

  Console.WriteLine(result);
}

 

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *