Unit 3
Unit 3
Exceptions are errors that occur during runtime and disrupt program flow.
Why Needed:
Basic Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Cleanup code here");
}
Types:
Keywords:
try {
checkAge(16);
} catch (ArithmeticException e) {
System.out.println(e);
}
4. Common Exceptions
ArithmeticException - divide by 0
5. Built-in Exceptions
Type Examples
Checked IOException, SQLException
Unchecked NullPointer, ArithmeticException
Error StackOverflowError, OutOfMemory
Example:
Diagram:
New
|
start()
|
Runnable
|
Running
|
sleep(), wait()
|
Terminated
Methods:
Example:
3.
Using Lambda Expression
Final Tips