C# Lab05
C# Lab05
Lab 5
Objectives:
At the end of this session, you will able to understand:
Namespace
Exception Handling
3
C# - Lab 5 – Namespace and Exception Handling
namespace Bai05
{
class Vidu1
{
static void Main(string[] args)
{
byte[] a = new byte[5];
//nhap mang
try
{
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("a[{0}]=", i + 1);
a[i] = Convert.ToByte(Console.ReadLine());
}
}
catch (FormatException ex)
{
//Console.WriteLine(ex.Message);
Console.WriteLine("Khong duoc nhap ki tu cho mang so");
}
catch (OverflowException ex)
{
//Console.WriteLine(ex.Message);
Console.WriteLine("Khong duoc nhap gia tri nam ngoai mien 0-
255");
}
catch (IndexOutOfRangeException ex)
{
//Console.WriteLine(ex.Message);
Console.WriteLine("Loi vuot qua pham vi cua mang");
}
4
C# - Lab 5 – Namespace and Exception Handling
//in mang
for (int i = 0; i < 5; i++)
Console.Write(" {0}", a[i]);
}
}
}
Exercise 3: Write a program to accept a number and print multiplication table of that number. Use exception
handling to ensure that user enters only numeric values and the number entered is greater than zero.
int x = 20 / value;
6
C# - Lab 5 – Namespace and Exception Handling
return x;
}
public static void Main()
{
int value = 0;
try
{
value = AnExceptionFunction(10); // This works ok
Console.WriteLine("Value = {0}", value);
AnExceptionFunction(0); // This doesn't
Console.WriteLine("Value = {0}", value);
}
catch (Exception e)
{
Console.WriteLine("Caught an exception {0}. Continuing", e);
}
Console.WriteLine("Done");
}
}
7
C# - Lab 5 – Namespace and Exception Handling
//mo file de ghi du lieu
outStream = File.OpenWrite("DestinationFile.txt");
//mo file de doc du lieu
inStream = File.OpenRead("BogusInputFile.txt");
//cac cau lenh doc du lieu tu file
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (outStream != null)
{
outStream.Close();
Console.WriteLine("outStream closed.");
}
if (inStream != null)
{
inStream.Close();
Console.WriteLine("inStream closed.");
}
}
}
}
Exercise 1:
Create a namespace called Customer and add a class to it having a method that accepts customer name.
Create another namespace called Order and two classes within it, one for grocery items and the other for
bakery products. The Main() program should accept customer name and a choice indicating whether the
customer has selected to order grocery items or bakery products. Accordingly, the appropriate class should
be called and a message displayed informing the user about the choice.
8
C# - Lab 5 – Namespace and Exception Handling
Exercise 2:
Write a custom exception named AmountException to handle the following business issues
When Senior Lecture gets less than 60,000 salary
When bonus is more than 10,000
Your exception class should have a field named personName to store the person's name.
Write a Test program to work with various class objects and their behaviours. Add some code to
demonstrate the polymorphism. Also show the functionality of your custom exception class by
adding some appropriate code.