Given below the sample code :
1 public class A {
2 static void test() throws Error{
3 if (true) throw new AssertionError();
4 System.out.print("test ");
5 }
6 public static void main(String[] args) {
7 try { test(); }
8 catch (Exception ex) { System.out.print("exception "); }
9 System.out.print("end ");
11}}
How can we correct the above code ?
1. No need of correction
2. By changing "Exception" class to "Error" at line 8 in "caught"
3. By changing "throws Error" to "throws Exception" at line number 2.
4. By throwing exception in place of error at line 2.
(2)
Above code will give uncaught exception as output. Since it is of type "error " so it should be handled by error class object.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.