Рет қаралды 1,719
Core Java | Checked Vs Unchecked Exceptions | How to use in your Code?
#corejava #tamilkaruvoolam #exceptionhandling
Checked - Compile time (FileNotFoundException, MalformedURLException)
Unchecked - Runtime (ArithmeticException, NullPointerException)
Source Code-1 for Unchecked Exception
/**
Demo Class for Exception Handling
@author Anthoniraj Amalanathan
@since 30-Sep-2022
*/
class ArithmeticLibrary {
public static double divide(int a, int b) {
return (a / b);
}
}
public class ArithmeticLibraryTest {
public static void main(String[] args) {
try {
System.out.println(ArithmeticLibrary.divide(10, 0)); // Unchecked
} catch (ArithmeticException ae) {
System.out.println(ae.toString());
}
}
}
Source Code-2 for Checked Exception
public class URLTest {
public static void main(String arg[]) {
try {
URL url = new URL("www.anthoniraj.in");
/* reading html page */
} catch (MalformedURLException e) {
System.out.println(e.toString());
}
}
}