Exceptions in Java

Java Errors and ExceptionsErrorsExceptionsCompile/Build Time Errors vs Runtime ErrorsCompile-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 exceptionsChecked exceptions- Must be caught in try/catch blockor- 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 crashif you do not handle themFinally BlockFinally block always executes, even after the catch statement// Checked Exceptionpublic void readFile(String filePath) throws IOException {FileReader file = new FileReader(filePath);BufferedReader fileInput = new BufferedReader(file);// Read filefileInput.close();}// Unchecked Exceptionpublic void divide(int a, int b) {// Potential ArithmeticExceptionint result = a / b;}- Example:- Example:- Custom Checked Exceptions:- Extend the class.- Custom Unchecked Exceptions:- Extend the class.RuntimeExceptionException- must be handled or declared.- do not need to be handled or declared.- Example:- Example:Finally block always executes, even overrides any return statementsReturns 2 😱😱😱  Java Exception HierarchyThrowableErrorExceptionRuntimeExceptionUncheckederrorsUncheckedexceptionsCheckedexceptionsinternal errors that are rare andcan't usually be recovered from- OutOfMemoryError- VirtualMachineError- StackOverflowError- UnknownErrorExamples:- NullPointerException- IllegalArgumentException- IndexOutOfBoundsException- Examples:- IOException- FileNotFoundException- SQLException- Examples:- Any class that extends Exceptionor its subclasses except RuntimeException- Any class that extendsRuntimeException or its subclassestry/catch- try: Block of code that might throw an exception.- catch: Block that handles the exception thrown by the try block.throwUsed to explicitly throw an exception in your code.throwsUsed to indicates that a method can throw one ormore 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 FileNotFoundExceptionRefer to theseparate lecturenote onJava ExceptionHierarchyThe caller of this method is then required to either:handle these exceptions using a try-catch blockorpropagate them further by declaring throws in its ownmethod signature.try-with-resources StatementFinally block always executes, even after the catch statementThis is the try-with-resources statement which is used to auto-closethe resources. FileReader and BufferedReader are closed after the try block.No need to call fr.close() and br.close() explicitly in the finally block.