Sorry but can you explain  why compilation that part :
 "However, if it will cause a compilation failure at line catch(WithdrawalException e){ of the main method because the compiler will notice that the code in the try block never throws WithdrawalException and the catch block is unreachable."
I mean by that logic - if WithdrawalException extends RuntimeException   than why compiler  doesn't say anything about "catch(WithdrawalException e){" in main method?   Because   method withdraw  throws its superclass  and not WithdrawalException    anyway.
			
			
									
									
						About Question enthuware.ocpjp.v11.2.3356 :
Moderator: admin
- 
				minajev3
 - Posts: 18
 - Joined: Fri Feb 05, 2021 3:37 am
 - Contact:
 
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocpjp.v11.2.3356 :
Compiler looks at the throw clause of a method to determine what exceptions can be thrown by that method. 
But RuntimeExceptions do not have to be declared in the throws clause. So, if WithdrawalException is a RuntimeException, the compiler cannot determine just by looking at the throws clause that the method does not throw WithdrawalException. Therefore, it has no option but to accept catch(WithdrawalException we) block.
If WithdrawalException is a checked exception (i.e. not a RuntimeException), then it must be declared in the throws clause and the compiler can check the throws clause to determine whether WithdrawalException can be thrown by a method or not. If that method's throws clause does not list WithdrawalException, then the compiler knows that that method can never throw WithdrawalException and so catch(WithdrawalException we) will be unreachable.
			
			
									
									
						But RuntimeExceptions do not have to be declared in the throws clause. So, if WithdrawalException is a RuntimeException, the compiler cannot determine just by looking at the throws clause that the method does not throw WithdrawalException. Therefore, it has no option but to accept catch(WithdrawalException we) block.
If WithdrawalException is a checked exception (i.e. not a RuntimeException), then it must be declared in the throws clause and the compiler can check the throws clause to determine whether WithdrawalException can be thrown by a method or not. If that method's throws clause does not list WithdrawalException, then the compiler knows that that method can never throw WithdrawalException and so catch(WithdrawalException we) will be unreachable.
Who is online
Users browsing this forum: Bing [Bot] and 7 guests