MODULE III: EXCEPTION HANDLING AND I/O
3.1. Exception Handling and User-Defined Exceptions
Aim:
To demonstrate exception handling in Java, specifically the creation and use of user-defined exceptions.
Algorithm:
1. Create a user-defined exception class InvalidAgeException.
2. Write a method that checks for valid age input (e.g., age > 0).
3. If the age is invalid, throw the custom exception.
Java Code:
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class ExceptionHandling {
public static void validateAge(int age) throws InvalidAgeException {
if (age < 1) {
throw new InvalidAgeException("Age cannot be less than 1");
} else {
[Link]("Valid Age: " + age);
}
}
public static void main(String[] args) {
try {
validateAge(0);
} catch (InvalidAgeException e) {
[Link]([Link]());
}
}
}
Output:
Age cannot be less than 1
3.2. Java Interface for ADT Stack
Aim: To illustrate the implementation of an Abstract Data Type (ADT) Stack using a Java interface, and to
demonstrate exception handling for stack overflow and underflow.
Algorithm:
1. Define an interface StackADT with methods like push(), pop(), peek(), and isEmpty().
2. Implement this interface using an array-based class Stack.
3. Provide necessary exception handling for underflow and overflow.
Java Code:
package stacktest;
interface StackADT {
void push(int item);
int pop();
int peek();
boolean isEmpty();
void display();
}
class Stack implements StackADT {
private int maxSize = 5;
private int[] stackArray = new int[maxSize];
private int top = -1;
public void push(int item) {
if (top >= maxSize - 1) {
[Link]("Stack Overflow");
} else {
stackArray[++top] = item;
}
}
public int pop(){
if(top==-1)
{
[Link]("Stack Underflow");
return -1;
}
else
return stackArray[top--];
}
public int peek() {
if (top == -1) {
[Link]("Stack is Empty");
return -1;
}
else {
return stackArray[top];
}
}
public boolean isEmpty()
{
return top == -1;
}
public void display()
{
for(int i=0;i<=top;i++)
{
[Link](stackArray[i]);
}
public class StackTest {
public static void main(String[] args) {
Stack s = new Stack();
[Link](10);
[Link](30);
[Link](24);
[Link](45);
[Link](20);
[Link](" Stack elements after push");
[Link]();
[Link]("top element:"+[Link]());
[Link]("popped element"+[Link]());
[Link]();
[Link]("popped element"+[Link]());
[Link]();
[Link]("popped element"+[Link]());
[Link]();
[Link]("popped element"+[Link]());
[Link]();
[Link]("popped element"+[Link]());
[Link]();
[Link]("Is stack empty? " + [Link]());
[Link]();
}
}
Output:
Stack elements after push
10
30
24
45
20
top element:20
popped element20
10
30
24
45
popped element45
10
30
24
popped element24
10
30
popped element30
10
popped element10
Is stack empty? true
3.3. File Handling in Java
Aim:
To demonstrate basic file handling operations in Java, including checking file existence, readability,
writability, and displaying file details.
Algorithm:
1. Read a file name from the user.
2. Check if the file exists and if it is readable/writable.
3. Display the file details, such as file type and size.
Java Code:
java
Copy
import [Link];
public class FileInfo {
public static void main(String[] args) {
File file = new File("[Link]");
if ([Link]()) {
[Link]("File exists");
[Link]("Readable: " + [Link]());
[Link]("Writable: " + [Link]());
[Link]("File Type: " + ([Link]() ?
"Directory" : "File"));
[Link]("File Size: " + [Link]() + " bytes");
} else {
[Link]("File does not exist");
}
}
}
Output:
File exists
Readable: true
Writable: true
File Type: File
File Size: 1024 bytes