Abstract Class, Method, Interface and Exception (1)
Abstract Class, Method, Interface and Exception (1)
Syntax:
Access_specifier abstract return type name(parameter list)
Note:
1) To implement abstract method we use abstract class
2) Instance of abstract can’t be created.
3) The definition of abstract method is implemented in child
class.
}
}
}
namespace abstractmethod
{
abstract class Program // parent class or base class
{
public void add(int a, int b)
{
int c;
c = a + b;
Console.WriteLine("sum=" +c);
}
public void sub(int a, int b)
{
int c;
c = a - b;
Console.WriteLine("sub=" + c);
}
public abstract void div(int a, int b);
public abstract void mul(int a, int b);
}
}
Interface: It’s a user defined data type, it contain
only abstract method.
Access modifier interface name
{
Abstract method
}
Note:
1) Default access modifier is public.
is abstract.
4) We cant declare any variable inside the interface
namespace @interface
{
interface itest1 //base
{
void add(int a, int b);
}
interface itest2 // base
{
void sub(int a, int b);
void add(int a, int b);
}
class Program: itest2, itest1 // child
{
public void add(int a, int b)
{
int c;
c = a + b;
Console.WriteLine("Sum=" + c);
}
public void sub(int a, int b)
{
int c;
c = a - b;
Console.WriteLine("Sum=" + c);
}
static void Main(string[] args)
{
Program p = new Program();
p.add(10, 23);
p.sub(25, 20);
Console.ReadLine();
}
}
}
EXCEPTION HANDLING:
In a class, when a run time error occurs
exception is responsible..
Abnormal termination stops so that
namespace exceptionhandling
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter the value of a and
b");
try
{
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a / b;
Console.WriteLine("division=" + c);
Console.ReadLine();
}
/*catch (DivideByZeroException ex1)
{
Console.WriteLine("division is not possible");
Console.ReadLine();
}*/
/*catch (OverflowException ex2)
{
Console.WriteLine(ex2.Message);
Console.ReadLine();
}*/
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
finally
{
Console.WriteLine("Thank you for using VS
software");
}
}
}
}
Exceptions provide a way to transfer control
from one part of a program to another.
C# exception handling is built upon four
public override bool Equals(object obj)
{
// This implementation handles a null obj argument.
Person p = obj as Person;
if (p == null)
return false;
else
return this.Name.Equals(p.Name);
}
}
Similarly, if a user tries to open a file that does not exist, you can
first call the File.Exists method to check whether the file exists and,
if it does not, prompt the user whether they want to create it.
SYSTEM FAILURES
A system failure is a run-time error that cannot
be handled programmatically in a meaningful
way.
For example, any method can throw an
divide by zero.
Finally block is executed
try
{
Console.Write("Enter a file name to write: ");
string fileName = Console.ReadLine();
file = new FileInfo(fileName);
file.AppendText("Hello World!")
}
catch(Exception ex)
{
Console.WriteLine("Error occurred: {0}", ex.Message );
}
finally
{
// clean up file object here;
file = null;
}
}
class Program {
static void Main(string[] args) {
Console.Write("Please enter a number to divide 100: ");
try {
int num = int.Parse(Console.ReadLine());
int result = 100 / num;
Console.WriteLine("100 / {0} = {1}", num, result);
}
catch(DivideByZeroException ex) {
Console.Write("Cannot divide by zero. Please try again.");
}
catch(InvalidOperationException ex) {
Console.Write("Invalid operation. Please try again.");
}
catch(FormatException ex) {
Console.Write("Not a valid format. Please try again.");
}
catch(Exception ex) {
Console.Write("Error occurred! Please try again.");
}
NESTED TRY
static void Main(string[] args)
{
var divider = 0;
try
{
try
{
var result = 100/divider;
}
catch
{
Console.WriteLine("Inner catch");
}
}
catch
{
Console.WriteLine("Outer catch");
}
}
THROW
An exception can be raised manually by
using the throw keyword. Any type of
exceptions which is derived
from Exception class can be raised using the
throw keyword.
Console.WriteLine(std.StudentName);
}
RE-THROWING AN EXCEPTION
You can also re-throw an exception from the
catch block to pass on to the caller and let
the caller handle it the way they want.
static void Main(string[] args)
{
try
{
Method1();
static void Method2()
} {
catch(Exception ex)
string str = null;
{
Console.WriteLine(ex.StackTrace);
try
} {
}
Throwing Objects
You can throw an object if it is either directly or
indirectly derived from the System.Exception class.
You can use a throw statement in the catch block
Catch(Exception e)
{
...
Throw e
}
using System;
class DivByZero : Exception {
public DivByZero()
{
Console.Write("Exception has occurred : ");
}
}
class Program {
public double DivisionOperation(double numerator, double
denominator)
{
// throw exception when denominator value is 0
if (denominator == 0)
throw new DivByZero();