exceptionchecked-exceptionsunchecked-exception

Which Exception get priority Checked or Unchecked ? and Why?


I written my own two customized Exception one is checked and other is unchecked when i'm executing my code is shows only checked Exception Why i'm not able to get unchecked Exception output??

    class Test {  
   public static void main(String args[]) throws CheckedException {
       int i=0;
       int j=0;
       if(i==0){
           throw new CheckedException("Got Checked Exception");
       }
       if(j==0){
           throw new UncheckedException("Got Unchecked Exception");
       }
   }
}
class CheckedException extends Exception{
    CheckedException(String s){
        super(s);
    }
}
class UncheckedException extends RuntimeException{
    UncheckedException(String s){
        super(s);
    }
}

Output of above program is : Got Checked Exception ,But i'm expecting both output Got Checked Exception && Got Unchecked Exception. What mistake i'm doing here? and How can i overcome this?


Solution

  • In your program you have used throws in main() method and you have initialized i=0 and j=0.

    the first if(i==0) satisfies and generated exception and program stopped. that is why second if condition part not executing.

    if you want to check second condition initialize, i with something other than 0

    like i=1 and execute

    You can also use separate try catch block to test both cases

    thank you