1/11
Looks like no tags are added yet.
Exception
An event occurring during program execution that disrupts the normal flow of instructions.
Checked exception
A category of exceptions that the compiler mandates must either be caught or declared with the throws keyword.
Unchecked exception
A runtime exception that the compiler does not require you to explicitly catch or declare.
Handling difference between checked and unchecked exceptions
Checked exceptions require mandatory handling or declaration at compile time, whereas unchecked exceptions do not.
Common examples of unchecked exceptions
NullPointerException, ClassCastException, and ArithmeticException.
Defining a custom exception
Create a new class that extends Exception or one of its relevant subclasses.
throw keyword
Explicitly triggers and raises an exception from within a method or block of code.
throws keyword
Specifies in a method signature which exceptions the method can propagate to its caller.
Purpose of a try block
Encloses a segment of code that has the potential to raise an exception.
Purpose of a catch block
Catches and handles a matching exception thrown by an associated try block.
Choosing try/catch over throws
Use try/catch to resolve the problem locally; use throws to pass handling responsibility back to the caller.
Testing whether a method throws an exception
Execute assertThrows() by supplying the expected exception class alongside the executable code.