0% found this document useful (0 votes)
13 views11 pages

Unit 5 (Windows Programming - BCS 501)

The document covers the creation and use of namespaces, interfaces, and exception handling in C#. It explains how to create a custom namespace and interface, as well as how to handle exceptions using try-catch blocks and custom exceptions. It also includes example programs demonstrating these concepts in a Windows application context.

Uploaded by

akankshachpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Unit 5 (Windows Programming - BCS 501)

The document covers the creation and use of namespaces, interfaces, and exception handling in C#. It explains how to create a custom namespace and interface, as well as how to handle exceptions using try-catch blocks and custom exceptions. It also includes example programs demonstrating these concepts in a Windows application context.

Uploaded by

akankshachpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit V

Namespace, Interface & Exception Handling

Creating & using Namespace (DLL library)


Namespace are used to group classes, methods and other code into a package for use in
many different files.
A namespace shall be viewed as a container for classes and interface. As we know, a folder
on our file system contains files. A namespace is like a folder. By placing the classes into
namespaces, we can group related classes together. We can also avoid the risk of name collisions.
To access the class of a namespace, we need to use namespacename.classname. We can
use using keyword so that we don't have to use complete name all the time.

Syntax –
namespace namespace_name
{
// Classes, Methods, etc.
}

Program-Design a windows application to demonstrate creating and using custom namespace


using DLL.

Step 1 – Create a new project of type Class Library named myNameSpace.cs and build it.

namespace myNameSpace
{
public class operation1
{
public int mul(int a, int b)
{
return a * b;
}
}
public class operation2
{
public int div(int a, int b)
{
return a / b;
}
}
}
1
Step 2 – Create a new project of type Windows Form Application named Custom_Namespace
and add reference of myNameSpace.dll file that already created (Project – Add Reference –
Browse – Project Name – bin – debug – myNameSpace.dll).

using myNameSpace;
namespace Custom_Namespace
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
private void btn_display_Click(object sender, EventArgs e)
{
int a = int.Parse(txt_a.Text);
int b = int.Parse(txt_b.Text);
operation1 obj1 = new operation1( );
operation2 obj2 = new operation2( );
txt_mul.Text = (obj1.mul(a, b)).ToString( );
txt_div.Text = (obj2.div(a, b)).ToString( );
}
private void btn_exit_Click(object sender, EventArgs e)
{
Application.Exit( )
}
}
}
Output:

2
Creating & using Interface
It is also a user defined data type like a class but can contain only abstract methods. The
default scope of the members of an interface is public where as its private in case of a class.
Every abstract method of an interface should be implemented by the child class of an
interface. Interface cannot contain any data member. By default, every method of an interface is
abstract so we don’t require using abstract keyword just like in case of abstract class. If required
an interface can inherit from another interface.

Syntax –
<Access_Specifier> Interface <Interface_Name>
{
// Declaration of abstract methods
}

Program – Design a windows application to demonstrate concept of an Interface.

Step 1 – Create an Interface file named InterfaceDemo.cs, (Project Menu - Add New Item -
Interface).

namespace InterfaceDemo
{
interface iInterface1
{
int add(int x, int y);
}
interface iInterface2 : iInterface1
{
int sub(int x, int y);
}
public class Sample : iInterface2
{
public int add(int x, int y)
{
return x + y;
}
public int sub(int x, int y)
{
return x - y;
}
}
}

3
Step 2 – Create a new Windows Form named InterfaceExample.cs, (Project Menu - Add
Windows Form)

namespace InterfaceDemo
{
public partial class InterfaceExample : Form
{
public InterfaceExample( )
{
InitializeComponent( );
}
private void btn_display_Click(object sender, EventArgs e)
{
int a = int.Parse(txt_a.Text);
int b = int.Parse(txt_b.Text);
Sample s = new Sample( );
txt_add.Text = (s.add(a, b)).ToString( );
txt_sub.Text = (s.sub(a, b)).ToString( );
}
private void btn_exit_Click(object sender, EventArgs e)
{
Application.Exit( );
}
}
}
Output:

4
Exception
Exception is a class, which is responsible abnormally termination of a program, whenever
runtime error is occurred. For example, Overflow Exception, Format Exception, DivideByZero
Exception and many more.
There are two types of exceptions,
1) System Exception
2) Custom Exception

a) Exception Handling
It is a process for stopping abnormally termination of a program, whenever a runtime error
is occurred.

b) Exception Mechanism

1) try-catch block
a) try block
A try block identifies a block of code for which particular exceptions is activated. It is
followed by one or more catch blocks.

b) catch block
Exception raised within try block can be handled using the catch block. Code in the catch
block will only execute when an exception occurs.

Syntax –
try
{
// code that may raise exceptions
}

catch (<Exception_Class> <parameter>)


{
// handle exception
}

Note – Keep variable declarations out of the try block so that they can be accessed in the catch
block.

5
Following is an example of throwing an exception when dividing by zero condition
occurs −
namespace tryCatch
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
private void btn_display_Click(object sender, EventArgs e)
{
try
{
int a = int.Parse(txt_a.Text);
int b = int.Parse(txt_b.Text);
int c = a / b;
txt_result.Text = c.ToString( );
}

catch (DivideByZeroException ex)


{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Program is completed");
}
}
}
Output:

6
Using Finally Block
The finally block must come after a try or catch block. The finally block will always be
executed whether or not an exception is thrown. The finally block is generally used for cleaning-
up code e.g. for disposing an unmanaged objects etc.
Syntax –
try
{
// code that may raise exceptions
}

catch (<Exception_Class> <parameter>)


{
// handle exception
}
finally
{
// statements to be executed at any cost
}

Program – Write a program to demonstrate finally keyword.

namespace finallyDemo
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
private void btn_display_Click(object sender, EventArgs e)
{
try
{
int a = int.Parse(txt_a.Text);
int b = int.Parse(txt_b.Text);
int c = a / b;
txt_result.Text = c.ToString( );
}

7
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
Finally
{
MessageBox.Show("Finally block is executed");

}
MessageBox.Show("Program is completed");
}
}
}

8
Custom Exception
We have seen built-in exception classes in the C#. However, you often like to raise
an exception when the business rule of your application gets violated. So, for this you can
create a custom exception class by deriving Exception or ApplicationException class.
The .Net framework includes ApplicationException class since .Net v1.0. It was
designed to use as a base class for the custom exception class. A program throws an
exception when a problem shows up. This is done using a throw keyword.

Syntax –

Step 1 – Define a custom exception


<Access_Specifier> class <custom_exception_name> : ApplicationException
{
public override string Message
{
get
{
return “error_message”;
}
}
}

Step 2 – Call a custom exception

<custom_exception_name> <object_name> = new <custom_exception_name>


throw <object_name>

Example- Create DivideByOddException, which does not allow odd number to use as
divisor for division operation

9
namespace customException
{
public class DivideByOddException : ApplicationException
{
public override string Message
{
get
{
return "Attempted to divide by odd number";
}
}
}
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
private void btn_display_Click(object sender, EventArgs e)
{
try
{
int a = int.Parse(txt_a.Text);
int b = int.Parse(txt_b.Text);
if (b % 2 == 1)
{
DivideByOddException dboe = new
DivideByOddException( );
throw dboe;
}
int c = a / b;
txt_result.Text = c.ToString( );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Program is completed");
}
}
}

10
Output:

11

You might also like