Create User-Defined Exceptions

User-Defined Exceptions in C# vb.net asp.net

Exception handling features help you deal with any unexpected or exceptional situations that occur when a program is running. If you want users to be able to programmatically distinguish between some error conditions, you should create your own custom exceptions. It will simplify and improve the error handling and thus increase the overall code quality. Generally, for large scale projects , one should define custom exceptions.

How to create a custom exception ?

The following program shows how to create a custom exception.

C# Custom Exception Handling

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int i = 0;
                int j = 0;
                int k = 0;
                j = 10;
                k = 0;
                i = j / k;
            }
            catch (Exception ex)
            {
                throw (new MyCustomException("You cannot divide a number by zeo"));
            }
        }
    }
    public class MyCustomException : System.Exception
    {
            public MyCustomException() : base() {}
            public MyCustomException(string message): base(message)
            {
                MessageBox.Show(message);
            }
    }
}

Explanation :

How to handle custom exception in c#
public MyCustomException() : base() {}

Throw exception with out message

public MyCustomException(string message): base(message)
{
    MessageBox.Show(message);
}

Throw exception with simple message

VB.Net Custom Exception Handling

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim k As Integer = 0
            j = 10
            k = 0
            i = j \ k
        Catch ex As Exception
            Throw (New MyCustomException("You cannot divide a number by zeo"))
        End Try
    End Sub
End Class
Public Class MyCustomException
    Inherits System.Exception
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal message As String)
        MyBase.New(message)
        MessageBox.Show(message)
    End Sub
End Class

Explanation :

How to handle custom exception in vb.net
Public Sub New()
    MyBase.New()
End Sub

Throw exception with out message

Public Sub New(ByVal message As String)
    MyBase.New(message)
    MessageBox.Show(message)
End Sub

Throw exception with simple message

Exception Handling

Exceptions are the occurrence of some condition that changes the normal flow of execution. In .NET languages , Structured Exceptions handling is a fundamental part of Common Language Runtime. More about.... Exception Handling

System level Exceptions Vs Application level Exceptions

Exceptions are provide a structured, uniform, and type-safe way of controlling both system level and application level abnormal conditions. Application exceptions can be user defined exceptions thrown by the application. System exceptions are common exceptions thrown by the CLR. More about.... System level Exceptions VS ..

Difference between Exception and Error

Exceptions are related to the application and an Error is related to the environment in which the application is running. More about.... Exception and Error

NEXT.....Throwing Exceptions