(A).Try, Catch and Finally Statements

Aim:                                            
         To perform the Exception handling concept in the java programming language.

Algorithm:

Step1: Create a new text document and type the java program for  implementing the
exception handling concept.

Step2: Create a parent class named “Examfinally”.

Step3: Intialize  the variable “a=65” as integer and “b=0” as integer in the main
program.

Step4: “try” block is the used to handling the errors, and “catch” block is used to
handling the errors.

Step5: “finally” block is used to execute the program for  run the program upto the   
   last line.

Step6: Save the program using “.java” command.


Step7: Compile and run the program using “javac” and  “java” commands.

Program

class Examfinally
{
public static void main(String arg[])
{
int a;
try
{
a=Integer.parseInt(arg[0]);
}
catch(ArithmeticException e)
{
System.out.println("Error:"+e);
}
catch(ArrayStoreException e1)
{
System.out.println("Error:"+e1);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Error:"+e2);
}
finally
{
System.out.println("I am Always Executed....");
}
}
}


OUTPUT:



(B).Throws Statement

Aim:  
         To perform the exception handling concept in the java programming language.

Algorithm:

Step 1:  Create a new text document and type the java program for implementing the 
exception handling concept.   

Step 2:  “import” keyword is used to import the java program.

Step 3:   Create a parent class named “ExamThrows”.

Step 4:   Intialize the variable  “v=0”  as integer and “ch” as character. 

Step 5:   “switch”  statement is used to check the option cases and print the option case   
              which is selected.   

Step 6:   Save the program using “,java” command. 

Step 7:   Compile and run  the program using “javac” and “java” commands.

Program

import java.io.*;
class ExamThrows
{
public static void main(String args[])throws IOException
{
int v=0;
char ch;
while((ch=(char)System.in.read())!='.')
{
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':v++;
System.out.println("numbers of vowels:"+v);
}
}
}
}

OUTPUT


Post a Comment

Previous Post Next Post