import java.util.*; public class ExcepDemo{ public static void main(String[]args){ demonstrateExceptions(); } public static void demonstrateExceptions(){ Scanner sc = new Scanner(System.in); try{ System.out.println("numerator"); int n1 = sc.nextInt(); System.out.println("Denominator"); int n2 = sc.nextInt(); int result = n1/n2; System.out.println("Result"+result); } catch(ArithmeticException e){ System.out.println("Arithmetic exception occured"+e.getMessage()); } catch(ArrayIndexOutOfBoundsException e1){ System.out.println("AIOOfBE"+e1.getMessage()); } catch(NullPointerException e2){ System.out.println("Null pointer exception occured"+e2.getMessage()); } catch(Exception e3){ System.out.println("Caught generic exception"+e3.getMessage()); } finally{ System.out.println("Always executed"); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter