Hello,
Please, this question is from the OCP Java SE 6 Programmer Practice Exams textbook, Assessment Test 2 question 8. Below is the code for the question.
public class Salmon extends Thread { public static long id; public void run(){ for(int i=0; i<4;i++){ if(i==2 && id==Thread.currentThread().getId()){ new Thread(new Salmon()).start(); throw new Error(); } System.out.print(i + " "); System.out.println("programme got past Error!!"); } } public static void main(String[] args) { Thread t1 = new Salmon(); id = t1.getId(); t1.start(); } }
When I ran the code, below is my output:
My questions are as follow:0
programme got past Error!!
1
programme got past Error!!
Exception in thread "Thread-0" java.lang.Error
at org.OCP.JSE6.Exercises.Assessment1.Salmon.run(Salm on.java:15)
0
programme got past Error!!
1
programme got past Error!!
2
programme got past Error!!
3
programme got past Error!!
1.why was there an exception in thread after i was 1 and then the programme continued again. I just want to understand the output.
2. I thought since there was an exception, the programme should crash but that wasn't the case, the programme continued after the new Error() statement. Why is this?
Thanks