Advertisement
Advertisement
//C# Throwing user-defined exceptions
using System;
//Creating our own exception class by extending Exception class
class myException1 : Exception
{
//Overriding the ToString() method
public override String ToString()
{
return "User-Defined Exception";
}
//Defining the Main() method
public static void Main(String[] ar)
{
//Creating an object of our exception class
myException1 ob= new myException1();
try
{
//Using throw keyword to throw an object of our exception class
//which will raise exception of our exception class type.
throw ob;
}
catch(myException1 e)
{
Console.WriteLine("Exception handled - "+ e);
}
}
}
Exception handled - User-Defined Exception
//C# Creating a user-defined defined exception class by extending the built-in ArithmeticException class
using System;
//Creating our exception class by inheriting the ArithmeticException class
class MyException1 : ArithmeticException
{
//Overriding the ToString() method
public override String ToString()
{
return "User-Defined Exception by extending ArithmeticException";
}
//Defining the Main() method
public static void Main(String[] ar)
{
//Creating an object of our exception class
MyException1 ob= new MyException1();
try
{
//Using throw keyword to throw an object of our exception class
//which will raise exception of our exception class type.
throw new MyException1();
}
catch(MyException1 e)
{
Console.WriteLine("Exception handled - "+ e);
}
}
}
Exception handled - User-Defined Exception by extending ArithmeticException
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement