CPIT-305
Syllabus
Calendar
Notes & Examples
Project
Labs
Lab-1
Lab-2
Lab-3
Lab-4
Lab-5
Lab-6
Lab-7
Lab-8
Miscellaneous
MS Teams
GitLab
Edit Page
Exceptions in Java
Java Errors and Exceptions
Errors
Exceptions
Compile/Build Time Errors vs Runtime Errors
Compile-Time Errors: Syntax errors detected during compilation.
Runtime Errors: Errors that occur during program execution.
Process Exit Codes
- program: lines of codes
- process: a running program
- Each process terminates with an exit code (number)
- 0 => successful execution
- Non-Zero => The process Failed/Crashed
- Fatal! Serious runtime problems,
usually unrecoverable problems
(e.g., OutOfMemoryError).
- You can't try and catch those!
- Runtime recoverable problems,
(e.g., IndexOutOfBoundsException)
- You can try and catch those!
Unchecked exceptions
Checked exceptions
- Must be caught in try/catch block
or
- thrown/declared in the method signature
- program will NOT compile if you do not handle them
- don't require explicit handling
(try/catch nor thrown)
- program will compile but may crash
if you do not handle them
Finally Block
Finally block always executes, even after the catch statement
// Checked Exception
public void readFile(String filePath) throws IOException {
FileReader file = new FileReader(filePath);
BufferedReader fileInput = new BufferedReader(file);
// Read file
fileInput.close();
}
// Unchecked Exception
public void divide(int a, int b) {
// Potential ArithmeticException
int result = a / b;
}
- Example:
- Example:
- Custom Checked Exceptions:
- Extend the class.
- Custom Unchecked Exceptions:
- Extend the class.
RuntimeException
Exception
- must be handled or declared.
- do not need to be handled or declared.
- Example:
- Example:
Finally block always executes, even overrides any return statements
Returns 2 😱😱😱
Java Exception Hierarchy
Throwable
Error
Exception
RuntimeException
Unchecked
errors
Unchecked
exceptions
Checked
exceptions
internal errors that are rare and
can't usually be recovered from
- OutOfMemoryError
- VirtualMachineError
- StackOverflowError
- UnknownError
Examples:
- NullPointerException
- IllegalArgumentException
- IndexOutOfBoundsException
- Examples:
- IOException
- FileNotFoundException
- SQLException
- Examples:
- Any class that extends Exception
or its subclasses except RuntimeException
- Any class that extends
RuntimeException or its subclasses
try/catch
- try: Block of code that might throw an exception.
- catch: Block that handles the exception thrown by the try block.
throw
Used to explicitly throw an exception in your code.
throws
Used to indicates that a method can throw one or
more types of exceptions.
private static void checkedExceptionWithTryCatch() {
File file = new File("not_existing_file.txt");
}
try {
FileInputStream stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
private static void checkDepartment(String dept) {
if (dept != "CS"){
}
}
throw new IllegalArgumentException("Non CS department");
private static void checkedExceptionWithThrows() {
File file = new File("not_existing_file.txt");
}
FileInputStream stream = new FileInputStream(file);
throws FileNotFoundException
Refer to the
separate lecture
note on
Java Exception
Hierarchy
The caller of this method is then required to either:
handle these exceptions using a try-catch block
or
propagate them further by declaring throws in its own
method signature.
try-with-resources Statement
Finally block always executes, even after the catch statement
This is the try-with-resources statement which is used to auto-close
the resources. FileReader and BufferedReader are closed after the try block.
No need to call fr.close() and br.close() explicitly in the finally block.