Unit summary
- A package groups related classes, prevents name conflicts, controls access and maps to a directory.
- System packages such as java.lang, java.util and java.io provide the Java API; java.lang is imported automatically.
- User-defined packages are created with a package statement and used with import.
- An exception is an object describing an abnormal event; Throwable splits into Error and Exception.
- Checked exceptions must be caught or declared; unchecked ones derive from RuntimeException.
- try, catch and finally handle exceptions; multiple catch, nested try and finally control the flow.
- throw raises an exception, throws declares one, and user-defined exceptions extend Exception.
Glossary
| Term | Meaning |
|---|---|
| Access modifier | A keyword controlling visibility. |
| catch | A block that handles an exception. |
| Checked exception | An exception checked by the compiler. |
| CLASSPATH | The list of places where Java finds classes. |
| Exception | An object describing an abnormal event. |
| finally | A block that always runs. |
| Fully qualified name | A class name with its package. |
| import | A statement that makes classes usable by short name. |
| Package | A named group of related classes. |
| Stack trace | The list of method calls at the time of an exception. |
| System package | A package of the Java standard library. |
| throw | A statement that throws an exception. |
| Throwable | The superclass of all errors and exceptions. |
| throws | A clause declaring exceptions. |
| try | A block containing code that may throw. |
| Unchecked exception | A RuntimeException subclass. |
| User-defined exception | An exception class written by the programmer. |
Question bank
Short Answer Questions (2 marks each)
- Define a package.
- Name four system packages of Java.
- How is a user-defined package created?
- What is the CLASSPATH?
- Define an exception.
- Distinguish checked and unchecked exceptions.
- What is the use of finally?
- Distinguish throw and throws.
- What is a nested try?
- How is a user-defined exception created?
Medium Answer Questions (5 marks each)
- Explain the benefits of packages and the system packages.
- Explain how to create and access a user-defined package.
- Explain access protection in Java.
- Explain the exception hierarchy.
- Explain multiple catch statements with an example.
- Write a program with a user-defined exception.
Long Answer Questions (15 marks each)
- Explain packages: basics, system packages, creating, accessing and adding classes to user-defined packages.
- Explain exceptions, their types and the exception hierarchy.
- Explain try, catch, finally, multiple catch and nested try, with examples.
- Explain throw, throws and user-defined exceptions, with examples.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit IV
