BIT401 · 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.
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]
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
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.