throw
and throws
are the two keywords used to declare an exception in Java. They are very useful for programmers who have to handle exceptions.
class Code{void checkMarks(int marks){if(marks<50)throw new ArithmeticException("Exam failed");elseSystem.out.println("Exam passed");}public static void main(String args[]){Code object = new Code();object.checkMarks(43);}}
class Code{int checkMarks(int marks) throws ArithmeticException{int result= marks/0;return result;}public static void main(String args[]){Code object = new Code();try{System.out.println (object.checkMarks(43));}catch(ArithmeticException except){System.out.println("Error in dividing number by zero");}}}
Free Resources