Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PROGRAM FOR USER DEFINED EXCEPTION

PROGRAM
import java.io.*;
class AdditionExp extends Exception
{
 AdditionExp(String msg)
 {
	super(msg);
 }
}
class MultiplicationExp extends Exception
{
 MultiplicationExp(String msg)
 {
  super(msg);
 }
}
class MatrixExp
{
 public static void main(String arg[])
 {
  int i,j,k,r1,c1,r2,c2,op=0;
  int a[][]=new int[3][3];
  int b[][]=new int[3][3];
  int c[][]=new int[3][3];
  char co='b';
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  do
  {
  System.out.println("MENU\n\n1.Multiplication\n2.Addition\n");
  System.out.println("Enter ur choice");
  try
  {
   op=Integer.parseInt(br.readLine());
  }
  catch(Exception e)
  {
  }
  if(op==1)
  {
   try
   {
    System.out.println("Enter order of first matrix");
    r1=Integer.parseInt(br.readLine());
    c1=Integer.parseInt(br.readLine());
    System.out.println("Enter order of second matrix");
    r2=Integer.parseInt(br.readLine());
    c2=Integer.parseInt(br.readLine());
    if(r1!=r2)
    {
	throw new MultiplicationExp("Multiplication is not possible");
    }
    else
    {
     System.out.println("Enter a matrix order"+r1+"*"+c1);
     for(i=0;i
OUTPUT

MENU

1.Multiplication
2.Addition

Enter ur choice
1
Enter order of first matrix
2
2
Enter order of second matrix
2
3
Enter a matrix order2*2
4
3
2
1
Enter a matrix of order2*3
4
3
5
3
2
1
Resultant matrix is
25 18 23
11 8 11
Do U want to continue(y/n)
y
MENU

1.Multiplication
2.Addition

Enter ur choice
2
Enter order of first matrix
2
3
Enter order of second matrix
2
1
Caught addition exception
Addition is not possible
Do U want to continue(y/n)
n

PROGRAM FOR USER DEFINED EXCEPTION is a post from ShoutToWorld - Let's Learn Let's Shout - Helping bloggers and developers.



This post first appeared on ShoutToWorld, please read the originial post: here

Share the post

PROGRAM FOR USER DEFINED EXCEPTION

×

Subscribe to Shouttoworld

Get updates delivered right to your inbox!

Thank you for your subscription

×