What are exceptions?
Sometimes a method cannot do its job. The input is bad, a file is missing, or an action is not allowed. It needs a way to say so. An exception is an object that describes the problem and stops the normal flow of the method. Code can signal one with throw and handle it with catch. Or it can let the exception travel up the call stack (the chain of methods that called each other).
Throwing means: “I cannot finish. Someone at this level or above must decide what to do.” If nobody catches it, the current request or the whole program can end.
Key ideas
throw new SomeException("message"): create and throw an exception. Control leaves the method until something catches it.try { ... } catch (ExceptionType e) { ... }: run the code intry. If it throws that exception type, run thecatchblock instead. There you can log the error, turn it into another exception, or return a default value.- Checked vs unchecked: Some exceptions, such as
CloneNotSupportedException, are “checked.” The compiler forces you to either handle them or addthrowsto the method signature. Runtime exceptions, such asIllegalArgumentExceptionandUnsupportedOperationException, are “unchecked.” The compiler does not force anything.
Example: handling (try/catch)
@Override
public WelcomeEmail clone() {
try {
return (WelcomeEmail) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone failed", e);
}
}super.clone()can throwCloneNotSupportedException(checked). We catch it and wrap it in aRuntimeException(unchecked), with the original errorekept inside. Callers need nothrows.
Example: throwing (signaling error)
public static Logistics getLogistics(String mode) {
if (mode.equalsIgnoreCase("Air")) {
return new Air();
} else if (mode.equalsIgnoreCase("Road")) {
return new Road();
}
throw new IllegalArgumentException("Unknown logistics mode: " + mode);
}// LSP/ISP bad examples: unsupported operation
@Override
public void write() {
throw new UnsupportedOperationException("Can't write to a read-only file");
}- First: the argument is invalid (for example
"train"), so throwIllegalArgumentException. The code only reaches thethrowif neither branch matched. - Second: this type does not allow the operation, so throw
UnsupportedOperationException.
Where you see it in this repo
- Prototype:
clone()in try-catch forCloneNotSupportedException. - Factory / Abstract Factory:
IllegalArgumentExceptionfor unknown mode or unsupported region. - OCP BadCode:
IllegalArgumentExceptionfor unsupported payment method. - LSP/ISP BadCode:
UnsupportedOperationExceptionwhen a method is not supported, such as write on a read-only file or scan on a simple printer.