C# Workshop – Exceptions

Standard

Exceptions

Exceptions are errors caused by things that weren’t supposed to happen, but it is possible to handle when things don’t happen as expected in your programme.
This example will crash if incorrect input is entered, in dotnetfiddle.net enter the following:

using System;

public class Demo
{	
	public static void Main()
	{
		Console.WriteLine("Enter Number");
		int number = int.Parse(Console.ReadLine());
		Console.WriteLine(number);	
	}
}

Type Number and an exception will occur Input string was not in a correct format this is Unhandled as there’s nothing to cope with this happening and the programme crashes.
To handle an exception you need a try catch block where the try is the code you might have a problem with and a catch to do something when this exception happens.

To use a trycatch in dotnetfiddle.net enter the following:

using System;

public class Demo
{	
	public static void Main()
	{
		try
		{
			Console.WriteLine("Enter Number");
			int number = int.Parse(Console.ReadLine());
			Console.WriteLine(number);	
		}
		catch(FormatException ex)
		{
			Console.WriteLine("Input was not Valid");
		}
	}
}

In the example, there is a try block contains any code that might throw an Exception, if this does happen then the catch block code is run, there is a parameter for FormatException which is the type of exception expected if the input is invalid, it’s possible to get details of the error from the parameter if needed.
It’s also possible to throw your own exceptions if something occurs and your programme shouldn’t handle that situation itself but make the programme aware of this error instead.

To use a throw in dotnetfiddle.net by entering the following:

using System;

public class Demo
{	
	public static void Main()
	{
		Console.WriteLine("Enter Number between 1 and 10");
		int number = int.Parse(Console.ReadLine());
		if(number < 1 || number > 10)
		{
			throw new ArgumentOutOfRangeException("Number must be between 1 and 10");
		}
	}
}

When you throw an exception using the throw Keyword followed by the type of exception object in this case it is an ArgumentOutOfRangeException to indicate when the input was out of range, this example would also raise the System.FormatException as well.

Something to remember is that it is best to not throw a System.Exception, System.SystemException or ApplicationException but more specific ones like the one in the example and that you shouldn’t rely on Exceptions to do validation but use if to see if something is correct or not and use exception for those things that might happen such as you accept an int but the value entered is too high for that type.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s