+ 42
What is the point of throwing exceptions?
What is the point (benefit) of throwing exceptions manually? and it's advantage over just using an if statement
51 ответ
+ 83
Evaluation of thrown error codes within the catch statement allows for proper and well-organised code structure, instead of putting if statements all over the place whenever you need to check for exceptions.
+ 48
If you have a large application with thousands of lines of code one of the worst design choices is to silently catch everything so that no exceptions ever happen. When something doesn't work as intended (e.g. when a list of data is requested a blank screen is shown) it is an actual nightmare to debug. In contrast, using exceptions and getting the code to "fail fast" lets code break soon after an unintended action happens. When combined with meaningful error messages it is much easier to track down the bug
+ 25
@Jihad Kurdi
This depends entirely on your program design. Is the error fatal? Can the program continue to run after the error is met? Are we dealing with users (in the sense that we try to recover from an error if possible), or are we debugging our program (throwing specific statements will allow programmers to easily understand what went wrong, and where it occured).
Also, remember that throwing a code will cause the program execution to skip everything between the throw and the catch statement. This can prove to be useful in some cases.
+ 15
@Hatsy Rei
Thanks for the explanation ☺
+ 15
Well there is couple of reasons to catch/throw exceptions, just to mention:
- preventing some non-excepted users inputs and/or malicious code input
- preventing so called "loop of death" where your code might run in some loop forever and ever
- also, in some functions or calculations, related to results,
you can use catch/trow exceptions to inform user of what is going on,
and to ask for confirmation in order to continue or stop some procedure,
further calculation or some process controlled by your code running on some controller computer.
Just to to give some simple example. Let's say, it's almost everyday situation: The file called "bla bla.doc" already exists in your Document folder,
do you want to be overwritten with the same name file, or you want to give a new name to your coping file . . .
Something like that. Hope I helped a bit. ;-)
+ 13
@Garikai i didn't meant why exceptions are thrown by the compiler, i meant why do we throw them manually, look at the example i've given above and u will understand what i mean
+ 11
@Hatsy
i mean in the case below what is the benefit of throwing an exception and not just returning from the method and continuing to whatever the program is supposed to do.
void MyFunction(object obj) {
if(obj == null)
throw new NullReferenceExcpetion();
...
}
+ 11
@Dan Wlaker Thanks for your answer bro
+ 11
Lets say you have a function such as :
public void divideMe(int a, int b) throws IllegalArgumentException{
try {
a/b;
if (b == 0) {
throw new IllegalArgumentException("Divide by zero error");
}
. If a user puts 0, as the second parameter, the IllegalArgumentException will be thrown. This alerts the user of their input error.
+ 10
Thanks for your answers guys 😊 really appreciate it
+ 10
@Garikai Thanks for the explanation ☺
+ 10
Thank u guys for answering my question ☺
Seriously SL is a great Community 💛
+ 9
What is good @Aiman Naji? 🤔
+ 9
@Kirk Schafer thanks alot for the effort of making the code 😊
+ 7
@Hy You
yup it helped 😊
thanks for the explanation
+ 7
@Jenny thanks for the answer
+ 7
@Damien
do you know what the question really means?
+ 6
Throw is very useful when you're making libraries. Throwing allows the user of the library/code to handle the exception in their own way instead of defining what to do yourself.
It also allows you to run different code depending where the exception has occurred in your program.
+ 6
There's a good community here, Thanks, guys.
+ 6
I've seen exceptions used to unconditionally branch to another part of a program (either as a clean exit path or a GOTO).
This code iterates an infinite sequence of "new game objects" -- remembering state across games -- until the exception "StopIteration" is raised.
https://code.sololearn.com/cAP6JETxuE4r/?ref=app