Java For Android Tutorial #12: Throwing an exception - Illegalargumentex...


Code:
package com.example;public class java {
    public static void main(String[] str) {
        int marks = 91;
        if (marks < 0 || marks > 100){
                throw new IllegalArgumentException
                         (Integer.toString(marks));
        }
        else{
            System.out.println("Marks are okay");}}}

You can throw custom exceptions with/without meeting any condition. This is dangerous but presents great benefits, if handled properly. I have taken an example of a student. And if the marks does not lie in minimum and maximum range, it will throw an error. I have used illegal argument exception and if statement to prove this concept.

Comments

Popular Posts