Introduction To Java Programming Language
105606Module 4: Exception Handling
1(f). Which keyword explicitly generates an exception? (i) throws (ii) exception (iii) throw (iv) catch20262m
Module 4: Exception Handling
View this question on its own page →Which keyword explicitly generates an exception?
(i) throws
(ii) exception
(iii) throw
(iv) catchWorked SolutionAnswer: (iii)
throwthrowexplicitly throws an exception object.throwsappears in a method declaration to announce exceptions that may propagate to the caller.if (age < 18) throw new IllegalArgumentException("Underage");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
View this question on its own page →Compare checked and unchecked exceptions using suitable examples. Analyse how the compiler treats each category and discuss appropriate handling strategies.
Worked SolutionChecked vs unchecked exceptions
Checked Unchecked Compiler requires catching or declaring them No compile-time catch/declare requirement Subclasses of ExceptionexcludingRuntimeExceptionRuntimeExceptionand its subclassesExample: IOException,SQLExceptionExample: NullPointerException,ArithmeticExceptionimport 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 catchExceptionindiscriminately.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
View this question on its own page →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.Worked SolutionJava thread life cycle
A Java thread may be
NEW,RUNNABLE(eligible to run, including running),BLOCKED,WAITING,TIMED_WAITING, orTERMINATED. The scheduler decides when a runnable thread gets CPU time.MermaidDiagramRendering diagram…Calling
start()schedules a new thread; callingrun()directly is an ordinary method call. A terminated thread cannot be restarted.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
View this question on its own page →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.
Worked SolutionThread priorities
Java priorities range from
Thread.MIN_PRIORITY(1) toThread.NORM_PRIORITY(5) andThread.MAX_PRIORITY(10). Set one usingthread.setPriority(7)and inspect it withgetPriority().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.