Introduction To Java Programming Language

105606
Back to Introduction To Java Programming Language

Module 4: Exception Handling

  1. 1(f). Which keyword explicitly generates an exception? (i) throws (ii) exception (iii) throw (iv) catch20262m

    Module 4: Exception Handling

    Which keyword explicitly generates an exception?

    (i) throws
    (ii) exception
    (iii) throw
    (iv) catch

    View this question on its own page →
    Worked Solution

    Answer: (iii) throw

    throw explicitly throws an exception object. throws appears in a method declaration to announce exceptions that may propagate to the caller.

    if (age < 18) throw new IllegalArgumentException("Underage");
    
  2. 6(b). Compare checked and unchecked exceptions using suitable examples. Analyse how the compiler treats each category and discuss appropriate handling strategies.20267m

    Module 4: Exception Handling

    Compare checked and unchecked exceptions using suitable examples. Analyse how the compiler treats each category and discuss appropriate handling strategies.

    View this question on its own page →
    Worked Solution

    Checked vs unchecked exceptions

    Checked Unchecked
    Compiler requires catching or declaring them No compile-time catch/declare requirement
    Subclasses of Exception excluding RuntimeException RuntimeException and its subclasses
    Example: IOException, SQLException Example: NullPointerException, ArithmeticException
    import java.io.IOException;
    class Example {
        static void read() throws IOException {
            throw new IOException("Read failed");
        }
        public static void main(String[] args) {
            try { read(); }
            catch (IOException e) { System.err.println(e.getMessage()); }
        }
    }
    

    Catch exceptions when recovery is possible, or declare checked exceptions with throws. Prevent unchecked exceptions through validation and correct program logic; do not catch Exception indiscriminately.

  3. 7(a). Construct a thread life-cycle diagram showing the major states through which a Java thread can pass. Analyse transitions caused by start(), scheduling, waiting and termination.20267m

    Module 4: Exception Handling

    Construct a thread life-cycle diagram showing the major states through which a Java thread can pass. Analyse transitions caused by start(), scheduling, waiting and termination.

    View this question on its own page →
    Worked Solution

    Java thread life cycle

    A Java thread may be NEW, RUNNABLE (eligible to run, including running), BLOCKED, WAITING, TIMED_WAITING, or TERMINATED. The scheduler decides when a runnable thread gets CPU time.

    MermaidDiagram
    Rendering diagram…

    Calling start() schedules a new thread; calling run() directly is an ordinary method call. A terminated thread cannot be restarted.

  4. 7(b). A Java application creates several threads having different priorities. Analyse how thread priority is assigned and discuss why priority alone should not be treated as a guarantee of execution order.20267m

    Module 4: Exception Handling

    A Java application creates several threads having different priorities. Analyse how thread priority is assigned and discuss why priority alone should not be treated as a guarantee of execution order.

    View this question on its own page →
    Worked Solution

    Thread priorities

    Java priorities range from Thread.MIN_PRIORITY (1) to Thread.NORM_PRIORITY (5) and Thread.MAX_PRIORITY (10). Set one using thread.setPriority(7) and inspect it with getPriority().

    Priorities are hints to the scheduler, not a promise of execution order. Scheduling policies vary by JVM and operating system; threads may run in an order different from their priority. Correctness must rely on synchronization primitives (synchronized, locks, queues, latches), not priority. Avoid using priority as a substitute for coordination.