A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Handling and throwing exceptionsYou'll learn the basics of handling and throwing exceptions. Uses examples to demonstrate. |
On Wednesday, July 28th 2004 at 04:13 AM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.3 with 8 votes) |
|||||||||||||
|
This tutorial will teach you the basic things you need to know about exceptions. There's no need to download the project as all the code you need is in here, but some people like to have it: Exceptions are different from errors. Usually, an error is the result of an exception, as the exception is trying to make an error more easy to understand by the user who's running your application. Maybe you want the user to input a number, but instead he enters a character. You want to store the number in an int variable but if you try to store the character in the int variable you'll get anexception. Let's make a small application that does that. So start a new Console Application project in Visual Studio, and use the following code in Class1:
Compile the code, enter a number:
It works fine. Try entering a string now:
Here the application stops responding for a few seconds and here's our exception: ![]() Also the code that caused the exception is highlighted. Now what does this message say? We have an unhandled exception of type 'System.FormatException'. Here's how we handle this exception:
We use try & catch blocks. First we tell the application to try the block of code inside the { and }. catch says that if the code block inside try throws the exception System.FormatException, run the code inside it (i.e. catch). Run this piece of code and see the result for yourself:
This error is a hell of a lot nicer than the exception we got earlier. Now what if the user enters a number so big that the integer variable can't hold it. Signed integers can hold values from -2,147,483,648 to 2,147,483,647. So, what if some wise guy enters 6,000.000.000? Let's try this. When prompted enter this number (without the commas). We get a new exception: ![]() This exception is called OverflowException, it occurs when we try to enter a value that is too large/small for our data type. No problem, we handle it just like we did with FormatException:
And here's the result:
If you're some smart aleck I'm sure you'd say - How can I handle all the other exceptions? - similar, my conceited friend:
We can even display information about the unkown exception, and you'll usually want to do so. Replace the earlier addition to our code with this one:
I'm running out of exceptions here, so if you want to test our brand new addition to the code, remove one of the exception handlers, perhaps the overflow exception. Now compile and enter again the big number 6000000000. Because you deleted the block that handled OverflowException, it will be handled by the block that handles the rest of the exceptions:
Exception or not, sometimes you want the application to execute a block of code. Using the finally statement you can be sure that the piece of code inside it will always execute, even if an exception is thrown or not. You can add a finally statement after all of the try and catch statements:
Creating an exceptionYou can throw your own exceptions when something doesn't go the way you want it. Perhaps we don't want the user to enter the number '0' at the prompt. Let's replace the code inside try with the following:
Now try an run this. Enter '0' and see for yourself:
It works fine. You may now want to consider the tutorial named Creating custom exceptions. |
||||||||||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||||||||||
|
||||||||||||||
Current CommentsHi! Great article, it helped me alot!
You could also add an example how to repeat an exception instead of letting the program just end. For example: we need an integer input, but an string was entered... how to repeat the try - catch block?
Put everything in a method, and in the catch block you can recall the method (you can say the method calls itself).
by garphy on Sunday, November 20th 2005 at 05:46 PM
Hi! Great article, it helped me alot!
You could also add an example how to repeat an exception instead of letting the program just end. For example: we need an integer input, but an string was entered... how to repeat the try - catch block?
by Andrei Pociu on Monday, November 21st 2005 at 09:37 AM
Put everything in a method, and in the catch block you can recall the method (you can say the method calls itself).
Related Tutorials
Related Source Code
C# Job Search