Java Detailed Notes on Exceptions, Strings, File Handling, and Lambda Expressions
1. Types of Exceptions in Java
Java provides two main categories of exceptions:
a. Checked Exceptions (Compile-time Exceptions)
• These are exceptions that are checked at compile time.
• The compiler ensures these exceptions are either handled using try-catch or declared using
throws .
• Examples: IOException , SQLException , FileNotFoundException
b. Unchecked Exceptions (Runtime Exceptions)
• These are not checked during compilation.
• They are typically caused by logic errors or improper use of API.
• Examples: ArithmeticException , NullPointerException ,
ArrayIndexOutOfBoundsException
2. Exception Handlers in Java
Java provides the following constructs to handle exceptions:
• try : Used to define a block of code that may throw an exception.
• catch : Defines a block of code that handles a specific type of exception.
• finally : Defines a block that will always execute after try/catch regardless of an exception.
• throw : Used to manually throw an exception.
• throws : Declares the exception that a method might throw.
3. try and catch Blocks
• A try block contains code that might throw an exception.
• A catch block follows the try block and handles the exception.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero.");
}
1
4. Multiple catch Clauses
• You can have multiple catch blocks to handle different types of exceptions.
try {
int[] arr = new int[5];
arr[10] = 100;
} catch (ArithmeticException e) {
[Link]("Arithmetic error");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of bounds");
}
5. Nested try Statements
• try blocks can be nested inside other try blocks to handle multiple levels of exceptions.
try {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Handled inside inner try");
}
} catch (Exception e) {
[Link]("Handled in outer try");
}
6. throw, throws, and finally
• throw : Used to explicitly throw an exception.
throw new IllegalArgumentException("Invalid input");
• throws : Declares the exceptions that a method can throw.
2
void readFile() throws IOException {
// code that may throw IOException
}
• finally : A block that is always executed, even if an exception is not caught.
try {
// risky code
} finally {
[Link]("Always executes");
}
7. Creating Custom Exceptions
You can define your own exceptions by extending Exception class:
class MyException extends Exception {
MyException(String message) {
super(message);
}
}
8. String Class
• Strings are immutable in Java.
• Common methods: length() , charAt() , substring() , equals() , compareTo() ,
toUpperCase() , toLowerCase()
String s = "Hello";
[Link]([Link]());
9. StringBuffer Class
• Used for mutable strings.
• Thread-safe and synchronized.
• Common methods: append() , insert() , reverse() , delete()
3
StringBuffer sb = new StringBuffer("Hello");
[Link](" World");
10. StringBuilder Class
• Similar to StringBuffer but not synchronized.
• Suitable for single-threaded applications.
StringBuilder sb = new StringBuilder("Java");
[Link](" Programming");
11. Wrapper Classes
• Used to convert primitives into objects.
• Examples: Integer , Float , Double , Character
• Useful for collections and type conversion.
int a = 10;
Integer obj = [Link](a);
12. File Handling in Java
• Used to create, read, write, and delete files.
• Common classes: File , FileReader , FileWriter , BufferedReader , BufferedWriter
File f = new File("[Link]");
if ([Link]()) {
[Link]("File created");
}
13. Binary Streams
• Used to handle binary data like images, videos, etc.
• Classes: FileInputStream , FileOutputStream
4
FileInputStream fis = new FileInputStream("[Link]");
14. Character Streams
• Used to handle character/text data.
• Classes: FileReader , FileWriter , BufferedReader , BufferedWriter
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
15. Serialization
• Converts an object into a byte stream to save to disk or send over a network.
• The class must implement Serializable .
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](myObject);
16. Lambda Expressions
a. Introduction:
• Introduced in Java 8.
• Used to write inline implementations of functional interfaces (interfaces with one method).
b. Syntax:
(parameter) -> expression
c. Example:
Runnable r = () -> [Link]("Running thread");
5
17. Using Lambda Expressions
• Commonly used with Collections , Streams , and functional interfaces .
List<String> names = [Link]("John", "Jane", "Jack");
[Link](name -> [Link](name));
18. Method References
• A shorthand notation of a lambda expression to call a method.
[Link]::println
Types:
• Static methods: ClassName::staticMethod
• Instance methods: object::instanceMethod
• Constructor: ClassName::new