You are on page 1of 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserDefinedHandling
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
}
//Creation of User-defined Exception.
public class InvalidNameException :
ApplicationException
{
public InvalidNameException(string message)
: base(message)
{
}
}
class Names
{
public void showName(string correctName, string inputName)
{
if (correctName != inputName)
{
//this process will throw an error message to user defined
Exception you have created.
throw (new InvalidNameException("Invalid Name found"));
}
else
{
MessageBox.Show("Correct Name is: + correctName");
}
}
}

private void btnEvaluate_Click_1(object sender, EventArgs e)


{
Names NameVar = new Names();
//contents inside the try block will be checked if your codes have
errors.
//using try block is helpful to track errors
try
{
NameVar.showName(txtCorrectName.Text, txtInputName.Text);
}
//invalidNameException is the user defined Exception you've created.
//this is the declaration of the exception and the "ex" is the variable
for the exception.
//catch block will trap the error if there is.
catch (InvalidNameException ex)
{
MessageBox.Show(ex.Message);
}
//the finally block will process ever the codes finally
{
MessageBox.Show("Thanks.");
}
}
}
}

You might also like