Try-Catch Example
public class TryCatchExample {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero!");
}
}
}
Try-Finally Example
public class TryFinallyExample {
public static void main(String[] args) {
try {
[Link]("Inside try block");
} finally {
[Link]("Finally block executed");
}
}
}
Multiple Try-Catch Example
public class MultipleTryCatch {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Arithmetic Exception");
}
try {
int[] arr = new int[2];
[Link](arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array Index Out Of Bounds");
}
}
}
Nested Try-Catch Example
public class NestedTryCatch {
public static void main(String[] args) {
try {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Inner Catch: Arithmetic Exception");
}
int[] arr = new int[2];
[Link](arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Outer Catch: Array Index Out Of Bounds");
}
}
}
Throw Keyword Example
public class ThrowExample {
public static void main(String[] args) {
int age = 15;
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
}
}
}
Throws Keyword Example
import [Link].*;
public class ThrowsExample {
public static void main(String[] args) throws IOException {
readData();
}
static void readData() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter something: ");
String data = [Link]();
[Link]("You entered: " + data);
}
}
Custom Exception Example
class MyException extends Exception {
public MyException(String msg) {
super(msg);
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
try {
throw new MyException("Custom Exception Thrown!");
} catch (MyException e) {
[Link]("Caught: " + [Link]());
}
}
}
Write and Read a File
import [Link].*;
public class FileWriteRead {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java File");
[Link]();
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
}
}
Read 2 Files and Write into 1 File
import [Link].*;
public class MergeFiles {
public static void main(String[] args) throws IOException {
BufferedReader br1 = new BufferedReader(new FileReader("[Link]"));
BufferedReader br2 = new BufferedReader(new FileReader("[Link]"));
FileWriter fw = new FileWriter("[Link]");
String line;
while ((line = [Link]()) != null)
[Link](line + "\n");
while ((line = [Link]()) != null)
[Link](line + "\n");
[Link]();
[Link]();
[Link]();
[Link]("Files merged into [Link]");
}
}
Read One File and Write into 2 Files
import [Link].*;
public class SplitFile {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
FileWriter fw1 = new FileWriter("[Link]");
FileWriter fw2 = new FileWriter("[Link]");
String line;
boolean toggle = true;
while ((line = [Link]()) != null) {
if (toggle)
[Link](line + "\n");
else
[Link](line + "\n");
toggle = !toggle;
}
[Link]();
[Link]();
[Link]();
}
}
Count Characters in a File
import [Link].*;
public class CharCount {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
int count = 0;
int ch;
while ((ch = [Link]()) != -1) {
count++;
}
[Link]();
[Link]("Total characters: " + count);
}
}
Multithreading with 10 Thread Methods
class MyThread extends Thread {
public void run() {
[Link]([Link]().getName() + " is running.");
[Link]("ID: " + [Link]());
[Link]("Priority: " + [Link]());
[Link]("State: " + [Link]());
[Link]("Alive: " + [Link]());
try {
[Link](100);
} catch (InterruptedException e) {}
[Link]("After sleep");
}
}
public class MultiThreadMethods {
public static void main(String[] args) throws InterruptedException {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]("Thread-1");
[Link]("Thread-2");
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Both threads finished");
}
}
Inter-process Communication (wait/notify)
class Shared {
synchronized void printTable() {
try {
wait();
} catch (InterruptedException e) {}
[Link]("Printing table...");
}
synchronized void completeTask() {
[Link]("Task done. Notifying...");
notify();
}
}
public class InterProcessCommunication {
public static void main(String[] args) {
Shared s = new Shared();
new Thread(() -> [Link]()).start();
try { [Link](1000); } catch (InterruptedException e) {}
new Thread(() -> [Link]()).start();
}
}