OOPS LAB 7 Final PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

WEEK- 7 

Name: D. Harshavardhan 
Class:BE-2/4;CSE-2 
Roll.no:160117733101 
Program1: 
Aim: To develop an Application to implement built-in exceptions(arithmetic and 
Arrayindexoutofbounds) i.e multiple exception handling. 
DESCRIPTION: 
In Java 7 it was made possible to catch multiple different exceptions in the same 
catch block. This is also known as multi catch. 
Algorithm : 
1.Start 
2. Create a class ExceptionDemo. 
3. Declare variables x, y and array a. 
4. Set x=10, y=0 and size of array equals to 5. 
5.Create a try block, declare an array of size 5 in it and try to print the 6 
th 

element, 
Divide number with 0. 
6.Create catch blocks for ArrayIndexOutOfBoundsException an 
ArithmeticException 
7.stop 
 
SOURCE CODE : 
import java.io.*; 
class ExceptionDem 

public static void main(String args[]) 

int x=10; 
int y=0; 
int a[]=new int[5]; 
class ExceptionDemo1 

public static void main(String args[]) 

int x=10; 
int y=0; 
int a[]=new int[5]; 
try{ 
System.out.println("Division is:"+x/y); 

catch(ArithmeticException e){ 
System.out.println("Division by zero is not possible.Try other than zero"); 

try{ 
a[7]=32; 
 

catch(ArrayIndexOutOfBoundsException e) 

System.out.println("Initializing members beyond the capacity of the array is 
not allowed!!"); 
}}} 
OUTPUT: 

 
Name: D. Harshavardhan 
Class:BE-2/4,CSE-2 
Roll.no:160117733101 
PROGRAM-2: 
Aim: To develop an application for rethrowing an exception 
Description: 
We all know that exceptions occurred in the try block are caught in catch block. 
Thus, caught exceptions can be re-thrown using throw keyword. Re-thrown 
exception must be handled somewhere in the program, otherwise program will 
terminate abruptly. 
Algorithm: 1. Start 
2.import java.io.package 
3.create a class RethrowDemo 
 
4.create a static method demo () and create a try and catch block 
inside it. In try block throw new NullPointerException 
5 declare main and call the method inside the try block and define 
catch block. 
6.stop 
Source code: 
import java.io.*; 
class RethrowExceptionDemo 

public static void divide() //declaring divide method exception 

int x=10; 
int y=0; 
try{ 
System.out.println("Division is:"+x/y); 

catch(ArithmeticException e){ 
//System.out.println(e); 
System.out.println("Division by zero is not possible.Try other than zero"); 
System.out.println("__________________________________________________ 
___"); 
throw(e); 

 

public static void main(String args[]) 

try{ 
divide(); 

catch(ArithmeticException e){ 
System.out.println("Rethrown ArithmeticException in main"); 
System.out.println(e); 



OUTPUT: 

 
Name: D. Harshavardhan 
Class:BE-2/4;CSE-2 
Roll.no:160117733101 
 
PROGRAM-3: 
Aim: Develop an application to create an user defined exception to handle 
dividebyzero exception and arrayoutofbounds exception. 
Description: 
In java we can create our own exception class and throw that exception using 
throw keyword. These exceptions are known as user defined or custom exceptions. 
In this tutorial we will see how to create your own custom exception and throw it 
on a condition. 
Algorithm: 1. Start 
2.import java.io package 
3.create a class A 
4.create a try block and divide a number with 0 in it 
5. create catch blocks for ArithmeticExceptions 
4.create a try block, declare an array of size 5 in it and try to print 
the 6 
th 

element 
5. create catch blocks for ArrayIndexOutOfBoundsException 
6.stop 
SOURCE CODE: 
import java.io.*; 
class MyException extends Exception 

String s; 
public MyException(String s) 
 

this.s=s; 

public String toString() 

return("The Exception is "+s); 


public class UserDefinedException 

public static void main(String args[]) 

int a[]=new int[5]; 
try{ 
int x=7, y=32; 
if(x > 4) { 
a[x]=y; 
throw new MyException("Array out of bound"); 

else { 
a[x] = y; 

 

catch(MyException exp) 
{System.out.println(exp);} 


OUTPUT: 

 
Name: D. Harshavardhan 
Class:BE-2/4;CSE-2 
Roll.no:160117733101 
PROGRAM-4: 
Aim: to develop an application to create user defined exception for Voting 
Description: 
In this program we create a userdefined exception called InvalidAgeException 
which is throwed when the age of the voter is less than18 
Algorithm: 1. Start 
2.create a class InvalidAgeException which extends Exception class 
and create a constructor in it 
3.create main class Test 
 
4.create a static method validate which throws the defined exception 
and declare the statements when the exception should be throwed 
5.declare main and create try and catch blocks 
6.stop 
Source code: 
import java.io.*; 
class InvalidAgeException extends Exception 

InvalidAgeException(String s) 

super(s); 

public String toString() 

return("Candidate's age is less than 18 years!!!"); 
}} 
class ValidateCandidate 

static void validate(int age) throws InvalidAgeException 

if (age<18) 
throw new InvalidAgeException("Not eligible"); 
else 
 
System.out.println(" Welcome!! You are eligible to vote man....");} 
public static void main(String args[]) 

try 

validate(17); 

catch(InvalidAgeException e) 

System.out.println(e); 



 
Name: D. Harshavardhan 
Class:BE-2/4;CSE-2 
Roll.no:160117733101 
 
PROGRAM-5: 
Aim: Develop an application to create userdefined exception for checking 
minimum balance in bank account. 
Description: 
This program is used for checking minimum balance in bank account of a user. 
This is written using user defined exception. 
Algorithm: 
1.Import packages java.io and java.util 2.Create class MininmumBalance which 
etends Exception and leave the constructor empty 3 .Create class bank 4. Initialize 
bal=60000. 5. Create a static method check_bal(money) which throws exception 
MinimumBalance 6. If bal-money is less than 25,000 then print Invalid 7. Else bal 
is equal to bal-money and print the remaining amount 8. In main create a try block 
scan money and call the method Check_bal(x) 9.If the amount to be withdrown is 
huge then it throws an exception it is caught in imediat catch 10. Sorry!! You 
cannot withdraw amount below Minimum Balance 11. Stop 
SOURCE CODE 
import java.io.*; 
class AccountException extends Exception{ 
String e; 
AccountException(String e) { 
this.e = e; 

 
public String toString() { 
return e; 


class Account 

double balance; 
Account(double balance) 

this.balance = balance; 

void getBalance() throws AccountException 

if(balance < 0) 

throw new AccountException("Negative Amount: "+balance); 

else if(balance >= 0 && balance <= 1000) 

throw new AccountException("Low Balance: "+balance); 

else 

 
System.out.println("Balance: "+balance); 


void setBalance(double balance) 

this.balance = balance; 


public class Balance 

public static void main(String args[]) 

Account a = new Account(1025); 
try 

a.getBalance(); 

catch(AccountException e) 

System.out.println(e); 



OUTPUT: 

 
 
Name: D. Harshavardhan 
Class:BE-2/4;CSE-2 
Roll.no:160117733101 
PROGRAM 6: 
Aim: To Develop an application to demonstrate CheckedExceptions. 
Description: 
In Java exceptions under Error and Runtime Exception classes are unchecked 
exceptions, everything else under throwable is checked. 
Algorithm: 
1. Start 
2. Import packages java.io and java.lang 
3. Create a class checked 
4. Create an object for file at certain location 
 
 
5. Input string into the file 
6. Close the file 
7. Stop. 
Source code: 
import java.io.*; 
import java.lang.*; 
class checked{ 
public static void main(String args[])throws IOException 

FileReader file = new FileReader("C:\\Users\\MCA\\Desktop\\cse295\\text.txt"); 
BufferedReader fileInput = new BufferedReader(file); 
for (int counter = 0; counter < 3; counter++) 
System.out.println(fileInput.readLine()); 
fileInput.close(); 


OUTPUT: 
 
Name: D. Harshavardhan 
 
Class:BE-2/4;CSE-2 
Roll.no:160117733101 
PROGRAM 7: 
Aim: : To develop an application throws keyword 
Description: 
The Java throws keyword is used to declare an exception. It gives an information 
to the programmer that there may occur an exception so it is better for the 
programmer to provide the exception handling code so that normal flow can be 
maintained. 
Algorithm: 
1.start 
2.import java.io.IOException class 
3.create a method m()which throws IOException 
4. reate a method n()which calls method m() 
5. create a method p() and declare try block to call method n() and catch block to 
handle the exception 
6.declare main and create object for the class Test and call the method p() 
7.stop 
Source code: 
import java.io.IOException; 
class Test 

void m()throws IOException { 
throw new IOException("device error");//checked exception 
 

void n()throws IOException{ 
m(); 

void p() { 
try 

n(); 

catch(Exception e) 
{System.out.println("exception handled");} 

public static void main(String args[]) 

Test obj=new Test(); 
obj.p(); 
System.out.println("normal flow..."); 


OUTPUT: 

You might also like