This is a mcq in my textbook. and i have got exam in a few hours!!!! do help. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

This is a mcq in my textbook. and i have got exam in a few hours!!!! do help.

Which of the following can throw an exception? A)methods with throws clause B) methods with try block i m confused in this two options...

17th Mar 2018, 6:05 AM
ADP
ADP - avatar
9 Answers
+ 5
The question itself is odd. Methods without throws clause and try blocks can still throw unchecked exceptions. If the question is talking solely about throwing checked exceptions (i.e. All exceptions under Throwable), then the method must either handle the exception using try-catch blocks or it must specify the exception using throws keyword. Assuming that the question *wants* the method to be able to throw an exception back to the caller, then handling it internally with a try-catch block would not be an option. The answer would be A, in this case.
17th Mar 2018, 8:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
I think A is correct. When you type throws .... it 100% throws exception. Method with try block will not 100% throw exception untill something goes wrong. For example, your method might be division. 5 / 0 will throw exception. But 4 / 2 will not.
17th Mar 2018, 6:37 AM
Sad
Sad - avatar
+ 1
It is thrown but try is not throwing it. Exception is thrown when there is problem in program. For example 10 / 0. And try catch is catching it only.
17th Mar 2018, 7:14 AM
Sad
Sad - avatar
+ 1
Okay. Thank you very much!!! @Tom @HastyRei (Luckily the question was not asked in my test :P)
17th Mar 2018, 1:44 PM
ADP
ADP - avatar
0
Does that mean that throw just tells u that the method will throw and what type of exception it will throw. and try block is the one that actually throws....or something like that?
17th Mar 2018, 6:54 AM
ADP
ADP - avatar
0
Try/catch is for catching errors. They dont throw errors. Errors are thrown, for example if I devide 10 by 0. ArithmeticException will be thrown.
17th Mar 2018, 7:05 AM
Sad
Sad - avatar
17th Mar 2018, 7:06 AM
Sad
Sad - avatar
0
@Tom again my book- " The try statement contains a block of statements that we want to monitor for exceptions. if a problem occurs during its execution , an exception is THROWN"
17th Mar 2018, 7:12 AM
ADP
ADP - avatar
- 1
@Tom the lines in my book say - "A throws clause can be used in a method declaration to INFORM that the code within the method MAY throw an exception " & "If an exception occurs in a method, write a try-catch block within the method that MAY GENERATE an exception."
17th Mar 2018, 6:52 AM
ADP
ADP - avatar