C# 12 Pocket Reference by Joseph Albahari

C# 12 Pocket Reference by Joseph Albahari

Author:Joseph Albahari
Language: eng
Format: mobi, epub
Publisher: O'Reilly Media
Published: 2023-10-27T00:00:00+00:00


Consider the following code:

int x = 3, y = 0;

Console.WriteLine (x / y);

Because y is zero, the runtime throws a DivideByZeroException and our program terminates. We can prevent this by catching the exception as follows:

try

{

int x = 3, y = 0;

Console.WriteLine (x / y);

}

catch (DivideByZeroException)

{

Console.Write ("y cannot be zero. ");

}

// Execution resumes here after exception...

NOTE

This is a simple example to illustrate exception handling. We could deal with this particular scenario better in practice by checking explicitly for the divisor being zero before calling Calc.

Exceptions are relatively expensive to handle, taking hundreds of clock cycles.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.