The Sample Java program given below will check you understanding about the Exception handling in Java and it also test how you will handle exceptions when it arises.
The Sample Java program given below will check you understanding about the Exception handling in Java and it also test how you will handle exceptions when it arises.Given below the sample code :
public class A {
static void test() throws Error{
if (true) throw new AssertionError();
System.out.print("test ");
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print("exception "); }
System.out.print("end ");
}}
What will be the out put of the following code ?
1. test exception end3. test
4.Gives compile error.
(4)
Above code will give uncaught exception as output. Since it is of type "error " so it should be handled by error class object.