+ 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...
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.
+ 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.
+ 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.
+ 1
Okay. Thank you very much!!! @Tom @HastyRei
(Luckily the question was not asked in my test :P)
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?
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.
0
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"
- 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."