2 Exception Handling And Input Output

Advanced Java Programming · Unit 2

Exception Handling and Input/Output

Exam-focused notes for Exception Handling and Input/Output (Advanced Java Programming, BIT401): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.

What this unit covers

  • Exception types and hierarchy
  • Try-catch-finally blocks
  • Custom exception throwing
  • File I/O operations
  • Reading from keyboard and writing to files
  • Random file access
  • Serialization and object persistence

Custom exception throwing

208210 marks

List some features of Java. What are the types of exception? Write a program to take input an integer from the user and if the given integer is negative throw the exception.[10]

Key features of Java include: 1. Object-Oriented: Java is purely object-oriented, based on classes and objects. Everything is an object except primitive data types. 2. Platform Independent: Java follows "Write Once, Run Anywhere" (WORA) principle. Code comp...

Full solved answer →

Random file access

20825 marks

How do you access file randomly? Give an example. [5]

Random file access (also called direct access or seek access) allows you to read or write data at any position in a file without having to read through all preceding data sequentially. This is in contrast to sequential access where you must read from the be...

Full solved answer →

Exception types and hierarchy

20805 marks

Describe about exception handlings with example. [5]

Exception handling is a mechanism in programming that allows a program to detect, manage, and respond to runtime errors (exceptions) gracefully without terminating abruptly. It enables the program to continue execution or terminate in a controlled manner wh...

Full solved answer →

Reading from keyboard and writing to files

05 marks

Write a java program to read content from keyboard and write it to the file named test.txt in D: drive. [5]

1. FileWriter: Opens file "D:\\test.txt" for writing (creates if doesn't exist) 2. BufferedWriter: Wraps FileWriter for efficient buffered output 3. InputStreamReader: Converts keyboard input stream to character stream 4. BufferedReader: Wraps InputStreamRe...

Full solved answer →