Handling Exception by Using a Try/Catch Block

C Sharp > Handling Exception by Using a Try/Catch Block
19.10.2016 12:50:40


Наиболее часто встречающиеся слова в статье:

[exceptions] [Exception] [NullReferenceException] [Handling] [Try/Catch] [try/catch] [different types]


Статья:

Handling Exception by Using a Try/Catch Block
 
•    Use try/catch blocks to handle exceptions
 
•    Use one or more catch blocks to catch different types of exceptions
 
try{
 
}
 
catch (NullReferenceException ex){
 
// Catch all NullReferenceException exceptions.
 
}
 
catch (Exception ex){
 
// Catch all other exceptions.
 
}
 
Пример
 
static void Divide(string si, string s2)
 
try {
 
int nl = int.Parse(sl); int n2 = int.Parse(s2); int n = nl / n2:
 
Console.WriteLine(n);
 
}
catch (FormatException ex)
 
{
Console.WriteLine("not a number”);
 
}