About This eBook by Bart De Smet

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Handling Exceptions

Handling exceptions is done by protecting a certain block of code using try and having an exception handler associated with it using catch. You saw examples of this earlier, but let’s take a closer look:

try{    PrintFile(path);}catch (FileNotFoundException ex){    Console.ForegroundColor = ConsoleColor.Red;    Console.WriteLine(ex.Message);    Console.ResetColor();}

Here we’re protecting the call to PrintFile against the FileNotFoundException. Of course, the protected block of code (following the try keyword) can consist of more than one line of code, but it’s a good practice not to protect too much code if you’re only after catching an exception from a specific call somewhere in there.

Note: ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required