Various compile time errors in Exception Handling
The various compile-time exceptions in Exception Handling are summarized below.
1-Unreported exception
2-Exception has been already been caught & unreachable statement
3-Exception is never thrown in body of the corresponding try block
4-Incompatible types found
5-try without a catch and finally
6-catch without try
7-finally without try
1-Unreported exception:
"These exceptions are thrown if the code does?t have any exception handling code for checked exceptions ."
Example: FileNotFoundException,IOException etc
Demo Example:
package com.navneet.javatutorial;import java.io.PrintWriter;
public class UnreportedExceptionDemo {
public static void main(String[] args) {
PrintWriter pw=new PrintWriter("D:\UnreportedExceptionDemo.txt");
pw.println("Unreported Exception Demo Example");}
}
O/P:
Exception in thread ?main? java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException at com.navneet.javatutorial.UnreportedExceptionDemo.main(UnreportedExceptionDemo.java:9)
2-Exception has been already been caught & unreachable statement:
"These exceptions are thrown if the code have multiple catch statement and is already handled by the first catch block."
Demo Example:
package com.navneet.javatutorial;public class ExceptionDemo {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
} catch (Exception ex) {} catch (ArithmeticException e) {
}
}}
O/P:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception at com.navneet.javatutorial.ExceptionDemo.main(ExceptionDemo.java:12)
3-Exception is never thrown in body of corresponding try block:
"These exceptions are thrown if the code in try block does not have any checked exceptions and the catch block have exception handling code for that checked exceptions."
Demo Example:
package com.navneet.javatutorial;import java.io.FileNotFoundException;
public class ExceptionDemo1 {
public static void main(String[] args) {
try{
System.out.println("Hello ");
}
catch(FileNotFoundException ex){}
}
}
O/P:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body at com.navneet.javatutorial.ExceptionDemo1.main(ExceptionDemo1.java:12)
4-Incomaptiable types found
"These exceptions are thrown if the code have type mismatch problem."
Demo Example:
package com.navneet.javatutorial;
import java.io.FileNotFoundException;
import java.util.Scanner;public class ExceptionDemo1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name1 = scan.nextLine();
String name2 = scan.nextLine();
int num1 = scan.nextLine();
double num2 = scan.nextLine();
}}
O/P:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from String to int
Type mismatch: cannot convert from String to double at com.navneet.javatutorial.ExceptionDemo1.main(ExceptionDemo1.java:13)
5-try without catch and finally:
"These exceptions are thrown if the code have combination mismatch of any try -catch-finally block.The rest of the below 6 & 7 like 5 type."
Demo Example:
package com.navneet.javatutorial;
public class TryWithoutCatchFinally {
public static void main(String[] args) {
try{
System.out.println("Hello");
}
}}
O/P:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "Finally" to complete BlockStatements at com.navneet.javatutorial.TryWithoutCatchFinally.main(TryWithoutCatchFinally.java:9)
6-catch without try:
Demo Example:
package com.navneet.javatutorial;
public class CatchWithoutTryAndFinally {
public static void main(String[] args) {
catch(Exception e){e.printStackTrace();
}
}}
O/P:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "catch", ( expected
Syntax error, insert "-> LambdaBody" to complete LambdaExpression
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete BlockStatements at com.navneet.javatutorial.CatchWithoutTryAndFinally.main(CatchWithoutTryAndFinally.java:6)
7-finally without try:
Demo Example:
package com.navneet.javatutorial;public class FinallyWithoutTry {
public static void main(String[] args) {
finally{System.out.println("Hello");
}
}}
O/P:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "finally", delete this token at com.navneet.javatutorial.FinallyWithoutTry.main(FinallyWithoutTry.java:6)