Experiment 3
Experiment 3
Objectives:
Exception handling strategies in Java.
Reading and writing files in Java.
-------------------------------------------------------------------------------------------------------------------------------------
Older programming languages such as C have some drawbacks in the way they hande exceptions. For
example, suppose the programmer wishes to open a file for processing:
1. The programmer may be unaware of the exceptional conditions. For example, the file to be opened
may not necessarily exist. The programmer therefore may not write the codes for testing whether the
file exists before opening the file.
2. Suppose the programmer is aware of the exceptional conditions, he/she may decide to finish the main
logic first, and write the exception handling codes later – this "later", unfortunately, usually never
comes. In other words, you are not forced to write the exception handling codes alongside the main
logic.
3. Suppose the programmer decides to write the exception handling codes, the exception handling
codes intertwine with the main logic in many if-else statements. This makes the main logic hard to
follow and the entire program hard to read.
Java overcomes these drawbacks by building the exception handling into the language rather than leaving it
to the discretion of the programmers:
1. You will be informed of the exceptional conditions that may arise in calling a method - exceptions are
declared in the method's signature.
2. You are forced to handle exceptions while writing the main logic and cannot leave them as an
afterthought - your program cannot be compiled without the exception handling codes.
3. Exception handling codes are separated from the main logic - via the try-catch-finally construct.
Page 1 of 16
When executing Java code, different errors can occur: coding errors made by the programmer, errors due to
wrong input, or other unforeseeable things. When an error occurs, Java will normally stop and generate an
error message. The technical term for this is: Java will throw an exception (throw an error).
NullPointerException: thrown by the JVM when your code attempts to use a null reference where an
object reference is required. For example,
String[] strs = new String[3];
System.out.println(strs[0].length());
Exception in thread "main" java.lang.NullPointerException
ClassCastException: thrown by JVM when an attempt is made to cast an object reference fails. For
example,
Object o = new Object();
Integer i = (Integer)o;
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to
java.lang.Integer
IllegalArgumentException: thrown programmatically to indicate that a method has been passed an illegal
or inappropriate argument. You could re-use this exception for your own methods.
Page 2 of 16
IllegalStateException: thrown programmatically when a method is invoked and the program is not in an
appropriate state for that method to perform its task. This typically happens when a method is invoked
out of sequence, or perhaps a method is only allowed to be invoked once and an attempt is made to invoke
it again.
NoClassDefFoundError: thrown by the JVM or class loader when the definition of a class cannot be
found. Prior to JDK 1.7, you will see this exception called stack trace if you try to run a non-existent
class. JDK 1.7 simplifies the error message to "Error: Could not find or load main class xxx".
The figure below shows the hierarchy of the Exception classes. The base class for all Exception objects
is java.lang.Throwable, together with its two subclasses java.lang.Exception and java.lang.Error
The Error class describes internal system errors (e.g., VirtualMachineError, LinkageError) that rarely
occur. If such an error occurs, there is little that you can do and the program will be terminated by the
Java runtime.
The Exception class describes errors caused by your program
(e.g. FileNotFoundException, IOException). These errors could be caught and handled by your program
(e.g., perform an alternate action or do a graceful exit by closing all the files, network and database
connections).
Example 1:
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
Page 3 of 16
} catch (Exception e) {
System.out.println("Something went wrong.");
}}}
Example 2:
class TestMultipleCatchBlock1{
public static void main(String args[]){
try{
int a[]=new int[5];
//a[5]=0;
//System.out.println("the value is "+a[5]);
a[5]=30/0; // try this line first; next comment it and uncomment the previous 2 lines
}
catch(ArithmeticException e){
System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("task 2 completed");}
System.out.println("rest of the code..."); }
}
Java finally block is a block that is used to execute important code such as closing connection, stream
etc. Java finally block is always executed whether exception is handled or not.
Example 3:
class TestFinallyBlock1{
public static void main(String args[])
{ try{ int data=25/0; System.out.println(data); // Error type is
} catch(NullPointerException e){System.out.println(e); // Catching the wrong error type
} finally{System.out.println("finally block is always executed");
} System.out.println("rest of the code..."); } }
Output:
finally block is always executed
Exception in thread main java.lang.ArithmeticException:/ by zero
Page 4 of 16
Example 4:
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
}
else {
System.out.println("Access granted - You are old enough!");
}}
public static void main(String[] args) {
checkAge(15); // Set age to 15 (which is below 18...)
}}
In this example, we have created the validation method that takes an integer value as a parameter. If the age
is less than 18, we throw the ArithmeticException otherwise print the message "Access granted …".
Example 5:
class TestExceptionPropagation1{
void m(){
int data=50/0; }
void n(){
m(); }
void p(){
try{ n();
}catch(Exception e){System.out.println("exception handled");}}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal flow..."); }
}
Page 5 of 16
The Throws Keyword
Syntax of java throws:
return_type method_name() throws exception_class_name{ //method code }
Example 6:
class M{
void method() throws ArithmeticException{
int x = 20 / 0; } } // method() causes a division-by-zero error, which is of type ArithmeticException
public class Testthrows2{
public static void main(String args[]){
try{ M m=new M(); m.method();}
catch(Exception e){System.out.println("exception handled");}
System.out.println("normal flow..."); }}
Example 7:
import java.io.IOException;
class Testthrows1{
void m() throws IOException{
// the code inside the body of method m() will now throw an exception that was declared by throws
throw new IOException("device error"); }
void n() throws IOException{ // run it first then remove “throws IOException” and note the error!
m(); }
void p(){ try{ n(); }catch(Exception e){
System.out.println("exception handled");} }
public static void main(String args[]){
Testthrows1 obj=new Testthrows1();
obj.p();
System.out.println("normal flow..."); } }
Output:
exception handled
normal flow...
Page 6 of 16
There are two separate actions in examples 6 and 7:
Action 1: The examples declared the exception that may occur inside the method body in the method
signature by specifying its type using throws.
Action 2: The examples handled the exception using try/catch; if the exception occurred inside the try
block, it would be handled inside the catch block.
Each method has to either declare the exception or handle it, even if the exception occurs within the stack of
called methods.
Example 8: Create a program that simulates withdrawing money from a bank account. Implement two
custom exceptions:
InsufficientFundsException: Thrown if the user tries to withdraw more money than the account balance.
NegativeAmountException: Thrown if the user tries to withdraw a negative amount of money.
Simulate this scenario in the program and ensure that exception chaining is used to link both exceptions
together in a meaningful way.
class BankAccount {
private double balance; // an attribute of class BankAccount
Page 7 of 16
System.out.println("Withdrawal successful. Remaining balance: " + balance);
}
}
}
try {
account.withdraw(-50);
} catch (NegativeAmountException e) {
System.out.println("Exception caught: " + e.getMessage());
} catch (InsufficientFundsException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}
Assertion
Example 9:
public class AssertionSwitchTest {
public static void main(String[] args) {
char operator = '%'; // assumed to be '+', '-', '*', or '/'
int operand1 = 5, operand2 = 6, result = 0;
switch (operator) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
default: assert false : "Unknown operator: " + operator; // not plausible here
}
System.out.println(operand1 + " " + operator + " " + operand2 + " = " + result);
}}
Assertions, by default, are disabled to ensure that they are not a performance liability in the production
environment. To enable assertion, use the runtime command-line option –enableassertions (or –ea).
Page 8 of 16
In the above example, "assert false" always triggers an AssertionError. However, the output depends on
whether assertion is enabled or disabled. To run the commands below use the command prompt (MS-DOS).
> javac AssertionSwitchTest.java // no option needed to compile
> java -ea AssertionSwitchTest.java // enable assertion
Exception in thread "main" java.lang.AssertionError: %
at AssertionSwitchTest.main(AssertionSwitchTest.java:11)
> java AssertionSwitchTest.java // assertion is disabled by default
5 % 6 = 0 // note that 0 was the value used to initialize the variable “result”
Page 9 of 16
Example 11: Writing to a File Using FileWriter:
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write("Hello, File Writer!"); //
} catch (IOException e) {
e.printStackTrace();
} }}
Page 10 of 16
Example 12:
public class BufferedInputStreamExample {
public static void main(String[] args) {
FileInputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
inputStream = new FileInputStream("/Users/CodingNomads/Desktop/input_output/file.txt");
bufferedInputStream = new BufferedInputStream(inputStream);
// this will hold the collection of bytes that you read in an array
byte[] buffer = new byte[5];
int bytesRead = 0;
// this while loop will read the file 5 bytes at a time
while ((bytesRead = bufferedInputStream.read(buffer)) != -1){
// print out bytes read to console
System.out.print(new String(buffer, 0, bytesRead));}
} catch (IOException exc) {
exc.printStackTrace();
} finally {
try { // If the file was not opened in the first place, attempting to close it would cause an exception
inputStream.close();
bufferedInputStream.close();
} catch (IOException ex) {
ex.printStackTrace(); } }}}
Example 13:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class BufferedInputStreamEx {
public static void main(String[] args) {
try {
Page 11 of 16
FileInputStream fis = new FileInputStream("D:\\myfile.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
bis.skip(5); // Skips 5 bytes from the buffered input stream.
System.out.println("Input stream after skipping first 5 bytes:");
int i = 0;
while ((i = bis.read()) != -1) {
System.out.print((char) i); }
bis.close();
}catch(Exception e)
{System.out.println(e); } }}
Page 12 of 16
Byte streams have the word stream in the class name.
Character streams have a read-length of 16 bits at a time.
Character streams are for reading character by character.
Character streams are required to ensure that each character in human languages is properly read by
the machine.
Character streams have either the word reader or writer in the class name.
If a file contains data in the form of a human language, you probably want to use a Character stream.
Page 14 of 16
Example 19: Reading a File Using Scanner:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerFileExample {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("example.txt"))) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine()); }
} catch (FileNotFoundException e) {
e.printStackTrace(); }}}
Assignments:
Q1\ Write a Java program that divides two numbers provided by the user. Implement exception handling to
avoid runtime errors if the user enters invalid input (such as dividing by zero or non-numeric input).
Q2\ Write a program that reads an array of integers from the user and performs operations such as accessing
elements at specific indices and dividing them by a user-provided number. Handle the following exceptions:
Page 15 of 16
ArrayIndexOutOfBoundsException: if the user tries to access an invalid index.
ArithmeticException: if the user tries to divide by zero.
InputMismatchException: if the user inputs invalid data.
Q3\ Create a program where an OrderProcessor class contains a method to process online orders. If the order
amount is greater than a limit, throw a custom exception OrderLimitExceededException. Implement another
method to handle multiple orders and propagate exceptions using the throws keyword.
Q4\ Write a Java program where you read two numbers from the user and perform two operations:
Use nested try-catch blocks to handle any exceptions that occur in both operations separately (e.g.,
ArithmeticException for division and InputMismatchException for invalid input).
Q5\ Write a Java program that reads a text file (input.txt) line by line and prints each line to the console.
Q6\ Write a Java program that copies the contents of one file (source.txt) to another file
(destination.txt).
Q7\ Write a Java program that reads a text file (document.txt) and counts the number of words in the file.
Q8\ Write a Java program that appends text to an existing file (log.txt). Each time the program runs, it
should append a new line with the current timestamp and a user-provided message.
Q9\ Write a Java program that searches for a specific word in a text file (document.txt). If the word is
found, print the line numbers where the word appears.
Q10\ Write a Java program that reads a text file (example.txt), reverses the content of the file, and writes the
reversed content to another file (reversed.txt).
Page 16 of 16