Menu
Question Index
...

What will be the output of the following program?

public class Voilators {
    static String judgement = "~";

    public static void main(String[] args) {
        check(0); check(1); check(2); check(3); check(4); check(5);
        System.out.println(judgement);
    }
    
    private static void check(int i) {
        try { rules(i); }
        catch(java.sql.SQLException ex)
        {
            judgement += "402~";
            if(ex instanceof RuntimeException) { judgement += "420~"; }
        }
        catch(RuntimeException re) { judgement += "520~"; }
        catch(Exception e) { judgement += "197~"; }
    }

    static void rules(int i) throws java.sql.SQLException
    {
        if(i == 0) throw new RuntimeException();
        if(i == 3) throw new Voilation197();
        if(i%2 == 0) throw new Voilation402();
    }
}

class Voilation420 extends RuntimeException {}
class Voilation402 extends java.sql.SQLException {}
class Voilation197 extends ArithmeticException {}


~520~402~420~520~402~420~
~520~402~520~402~
~402~402~520~402~
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java Throw Keyword - Java Throws Keyword

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App