|
Free
C# Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
|||||
Try ... Catch in C# .NET |
||||||
|
<< Continues from the previous lesson C# has some inbuilt objects you can use to deal with any potential errors in your code. You tell C# to Try some code, and if can't do anything with it you can Catch the errors. Here's the syntax: try In the code below, we're trying to load a text file into a RichTextBox called rtb: try The code we want to execute goes between the curly brackets of try. We know that files go missing, however, and want to trap this "File not Found" error. We can do that in the catch part. Note what goes between the round brackets after catch: System.Exception excep Exception is the inbuilt object that handles errors, and this follows the word System (known as a namespace). After System.Exception, you type a space. After the space, you need the name of a variable (excep is just a variable name we made up and, like all variable names, you call it just about anything you like). The code between the curly brackets of catch is this: MessageBox.Show(excep.Message); So we're using a message box to display the error. After the variable name (excep for us), type a dot and you'll see the IntelliSense list appear:
If you just want to display the inbuilt system message, select Message from the list. When the programme is run, you'll see a message box like this:
If you know the type of error that will be generated, you can use that instead: catch (System.IO.FileNotFoundException) To find out what type of error will be generated, use this: catch (System.Exception
excep) The message box will tell you what system error is being generated. You can then use this between the round brackets of catch. If you want to keep things really simple, though, you can miss out
the round brackets after catch. In the code below, we're just creating
our own error messages: try You can add more catch parts, if you want: try The reason you would do so, however, is if you think more than one error may be possible. What if the file could be found but it can't be loaded into a RichTextBox? In which case, have two catch blocks, one for each possibility. There's also a Finally part you can add on the end: try You use a Finally block to clean up. (For example, you've opened up a file that needs to be closed.) A Finally block will always get executed, whereas only one catch will. (NOTE: there is also a throw part to the catch blocks for when you want a more specific error, want to throw the error back to C#, or you just want to raise an error without using try catch. Try not to worry about throw.) We won't be using Try
Catch block too much throughout this book,
however, because they tend to get in the way of the explanations. But
you should try and use them in your code as much as possible, especially
if you suspect a particular error may crash your programme. <-- The Locals Window | C# Methods --> <--Back to the C# .NET Contents Page View all our Home Study Computer Courses
|