1
Reply

What are custom exceptions in C#?

Rahul  Pandey

Rahul Pandey

11y
1.2k
0
Reply

    Sometime we might required to provide run time error or exception which are not predefined by C# in that case we have to be go for custom exceptions for creating the custom exceptions declare one class and inherit from Application Exception because whatever the custom exception we are going to be create that should come in the category of Application Exception class which is inheriting Exception class (Base class for all the Exceptions) for Example see below code public partial class ExceptionEx4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { int empSalary =10000 ; if (empSalary < 14000) { throw new SalaryException("Employee Salary Is too Low"); } } catch (SalaryException ex) { Response.Write(ex.CustomMessage); } } } public class SalaryException : ApplicationException { private string _customMessage; public string CustomMessage { get { return this._customMessage; } private set { this._customMessage = value; } } public SalaryException(string message) { this._customMessage = message; } } For learning more about similar tyes of questions you can prefer the below link c# interview questions