public class DivideByZero { public static void main(String[] args) { int a = 10; int b = 0; try { int c = a / b; System.out.println(c); } catch (ArithmeticException e) { e.printStackTrace(); } } }
import java.io.FileInputStream; import java.io.FileNotFoundException; public class FileNotFound { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("sample.txt"); } catch (FileNotFoundException e) { e.printStackTrace(); } } }In this example, we are trying to create a FileInputStream object for a file that does not exist, which will throw a FileNotFoundException. We catch the exception using a try-catch block and print the stack trace of the error using the printStackTrace() method. The printStackTrace() method is part of the Throwable class in Java, which is contained in the java.lang package. Therefore, no external package library is required to use this method.