Menu
Topics Index
...
`

Throw keyword


If you need explanation Read this topic

If you need Answer Take test on this topic

Throw keyword
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 {}

A. ~520~402~420~520~402~420~
B. ~520~402~520~402~
C. ~402~402~520~402~
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Throw Keyword - Java Throws Keyword

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans : b

Posted by Ananda Kumar    2014-03-25 12:57:03


Answer is D : 520, 402,420,197,402,420 it will be !! intially when check(0 ) is called..runtimeexception catch is excuted ! dn when chck(1 ) is called nothng is done ! whn chck(2 ) is called since vol402 extends Sqlexcption the sqlexption catch is excuted and prints 402 and 420..then chck(3) vch extends ArthmeticExctpn vch inturn extends Excption 197 it prints..and thn same wd chck(4) and 5

Posted by Shaik Nyamath    2014-03-25 12:59:45


E. Compilation Error occurs. 'ex' is an instance of java.sql.SQLException and it is being compared as a subtype of RuntimeException which gives an error. If ex is tested with Exception or java.sql.SQLException using instanceof operator, then the program compiles.

Posted by Nikhil Sulibhavi    2014-03-25 13:11:20


E Compilation errors..we are comparing the ex with  Runtimeexception where ex as an java.sql.SQLException..both are inconvertable types..if ex is an RuntimeException than progrma wl compl

Posted by Uday Kumar    2014-03-25 14:47:59


Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-26 04:26:00


© meritcampus 2019

All Rights Reserved.

Open In App