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.
Exception Handling and Input/Output in short
Programs meet errors and must also read and write data. This chapter shows how Java handles run-time errors with exceptions and how it works with files and objects.
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
2082 · 10 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]
Show solved answerHide answer
Model Answer: Features of Java, Exception Types, and Exception Handling Program
Features of Java (3 marks)
Key features of Java include:
-
Object-Oriented: Java is purely object-oriented, based on classes and objects. Everything is an object except primitive data types.
-
Platform Independent: Java follows "Write Once, Run Anywhere" (WORA) principle. Code compiled to bytecode runs on any platform with JVM.
-
Simple and Familiar: Java syntax is similar to C/C++, making it easy to learn for programmers familiar with these languages.
-
Secure: Java provides built-in security features like bytecode verification, exception handling, and no pointer arithmetic.
-
Robust: Strong memory management, automatic garbage collection, and exception handling make Java robust.
-
Multithreaded: Java supports concurrent programming through built-in threading capabilities.
-
Dynamic: Java supports dynamic loading of classes at runtime.
Types of Exceptions (3 marks)
Exceptions in Java are classified as:
1. Checked Exceptions
- Exceptions that must be caught or declared in the method signature
- Checked at compile time
- Examples: IOException, SQLException, FileNotFoundException
2. Unchecked Exceptions
- Exceptions that need not be caught or declared
- Checked at runtime
- Derived from RuntimeException class
- Examples: NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException
3. Error
- Serious problems that should not be caught
- Examples: StackOverflowError, OutOfMemoryError
Program: Exception Handling for Negative Integer (4 marks)
import java.util.Scanner;
public class NegativeNumberException {
// Custom exception class
static class NegativeNumberException extends Exception {
public NegativeNumberException(String message) {
super(message);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter an integer: ");
int number = sc.nextInt();
// Check if number is negative and throw exception
if (number < 0) {
throw new NegativeNumberException("Error: Negative number not allowed!");
}
System.out.println("You entered: " + number);
} catch (NegativeNumberException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
sc.close();
System.out.println("Program terminated.");
}
}
}
Output Example:
Enter an integer: -5
Exception caught: Error: Negative number not allowed!
Program terminated.
Explanation:
- A custom exception class
NegativeNumberExceptionextendsException(checked exception) - User input is taken using Scanner
- If the number is negative, the exception is thrown with a message
- The
catchblock handles the thrown exception - The
finallyblock executes regardless of exception occurrence
Random file access
2082 · 5 marks
How do you access file randomly? Give an example. [5]
Exception types and hierarchy
2080 · 5 marks
Describe about exception handlings with example. [5]
Reading from keyboard and writing to files
0 · 5 marks
Write a java program to read content from keyboard and write it to the file named test.txt in D: drive. [5]
Make Unit 2 stick
Practice BIT401 with flashcards & quizzes