1.
Write a program to create a user defined exception
class InvalidAgeException extends Exception { public InvalidAgeException(String message) {
super(message); } } public class CustomExceptionExample { static void checkAge(int age) throws
InvalidAgeException { if (age < 18) throw new InvalidAgeException("Age must be 18 or above to
vote."); else [Link]("You are eligible to vote."); } public static void main(String[] args) {
try { checkAge(15); } catch (InvalidAgeException e) { [Link]("Exception caught: " +
[Link]()); } [Link]("Program continues..."); } }
2. Explain (a) Throws (b) Throw (c) Finally
(a) throws – declares that a method can throw an exception. (b) throw – explicitly throws an
exception. (c) finally – executes always, used for cleanup. Example: try { int result = 10 / 0; } catch
(ArithmeticException e) { [Link]("Error: " + e); } finally { [Link]("Finally
always executes."); }
3. Write a program to handle an exception using try…catch block
public class TryCatchExample { public static void main(String[] args) { try { int data = 50 / 0; } catch
(ArithmeticException e) { [Link]("Handled: " + e); } [Link]("Rest of the
code..."); } }
4. Java program to read character stream using FileInputStream
import [Link].*; public class ReadFileExample { public static void main(String[] args) throws
IOException { FileInputStream fin = new FileInputStream("[Link]"); int i; while ((i = [Link]()) !=
-1) [Link]((char)i); [Link](); } }
5. Explain exception handling mechanism and handle Arithmetic
and Array Index Out of Bound exception
try { int a = 10 / 0; int arr[] = new int[5]; [Link](arr[10]); } catch (ArithmeticException e) {
[Link]("Cannot divide by zero."); } catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of bound."); } [Link]("Program continues...");
6. Explain Random Access File and its methods
RandomAccessFile allows reading and writing at any position in a file. Common methods: seek(),
read(), write(), length(), getFilePointer() Example: RandomAccessFile raf = new
RandomAccessFile("[Link]", "rw"); [Link]([Link]()); [Link]("New Data"); [Link]();
7. What is Exception? Explain various techniques of exception
handling
Exception: An event that disrupts normal flow. Techniques: try–catch, throw, throws, finally,
user-defined exceptions.
8. Explain BufferedInputStream and BufferedOutputStream
BufferedInputStream adds buffering to improve efficiency of reading. BufferedOutputStream buffers
output before writing. Example: BufferedInputStream bin = new BufferedInputStream(new
FileInputStream("[Link]")); BufferedOutputStream bout = new BufferedOutputStream(new
FileOutputStream("[Link]"));
9. Write a Java program to write data to file using
FileOutputStream
import [Link].*; public class FileOutputStreamEx { public static void main(String[] args) throws
IOException { FileOutputStream fout = new FileOutputStream("[Link]"); [Link]("Hello
World!".getBytes()); [Link](); [Link]("Data written successfully!"); } }
10. Explain reading and writing with console I/O
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter name: "); String name = [Link](); [Link]("Welcome " +
name);
11. Program that handles multiple exceptions
try { int[] arr = new int[5]; arr[10] = 20 / 0; } catch (ArithmeticException e) {
[Link]("Arithmetic error"); } catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index error"); } catch (Exception e) { [Link]("General
exception"); }
12. Explain use of throw and throws in Java with example
throw – used to explicitly throw an exception. throws – used in method signature to declare possible
exceptions. Example: static void checkAge(int age) throws ArithmeticException { if(age<18) throw
new ArithmeticException("Access denied"); }
13. Explain Random Access File with program
RandomAccessFile raf = new RandomAccessFile("[Link]", "rw"); [Link]("File length:
" + [Link]()); [Link]([Link]()); [Link]("Added using RandomAccessFile"); [Link]();
14. Program using DataInputStream and DataOutputStream
DataOutputStream dout = new DataOutputStream(new FileOutputStream("[Link]"));
[Link](101); [Link]("Vinit"); [Link](); DataInputStream din = new
DataInputStream(new FileInputStream("[Link]")); [Link]([Link]());
[Link]([Link]()); [Link]();
15. Explain class hierarchy of exceptions with checked vs
unchecked
Throwable ■■■ Exception ■ ■■■ Checked (IOException, SQLException) ■ ■■■ Unchecked
(RuntimeException, NullPointerException) ■■■ Error (OutOfMemoryError, StackOverflowError)
16. Role of finally block
finally executes always, used to release resources. try { int a = 10/0; } catch (Exception e) {
[Link](e); } finally { [Link]("Finally always executes."); }
17. Explain BufferedReader and BufferedWriter with example
BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]")); [Link]("Hello");
[Link](); BufferedReader reader = new BufferedReader(new FileReader("[Link]"));
[Link]([Link]()); [Link]();
18. Program demonstrating multiple catch blocks
try { int a = 10/0; String s = null; [Link]([Link]()); } catch (ArithmeticException e) {
[Link]("Divide by zero"); } catch (NullPointerException e) { [Link]("Null
string"); } catch (Exception e) { [Link]("General exception"); }
[Link]("Program continues...");