In this article, I am going to explain to you how to write custom exception class or user-defined exception, the custom exception class should extend from Exception, here instead of using FileNotFoundException I am using custom new ExcelFileNotFoundException class
User-defined exception:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.keys.examples; public class ExcelFileNotFoundException extends Exception { private static final long serialVersionUID = 1L; public ExcelFileNotFoundException(String errorMessage) { super(errorMessage); } } |
Test class :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.keys.examples; import java.io.IOException; public class PDFManager { public PDFManager() { } public static void main(String[] args) throws IOException, Exception { try { ExcelReader.setExcelFile(); } catch (ExcelFileNotFoundException e) { e.printStackTrace(); } } } |
Output :