What is the point of throwing exceptions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Aug 2017, 12:43 PM
Jihad Naji
Jihad Naji - avatar
51 Answers
+ 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.
3rd Aug 2017, 12:55 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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
3rd Aug 2017, 1:14 PM
Dan Walker
Dan Walker - avatar
+ 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.
3rd Aug 2017, 1:14 PM
Hatsy Rei
Hatsy Rei - avatar
+ 15
@Hatsy Rei Thanks for the explanation ☺
3rd Aug 2017, 1:24 PM
Jihad Naji
Jihad Naji - avatar
+ 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. ;-)
4th Aug 2017, 7:21 PM
Hy You
+ 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
4th Aug 2017, 10:20 AM
Jihad Naji
Jihad Naji - avatar
+ 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(); ... }
3rd Aug 2017, 1:09 PM
Jihad Naji
Jihad Naji - avatar
+ 11
@Dan Wlaker Thanks for your answer bro
3rd Aug 2017, 1:25 PM
Jihad Naji
Jihad Naji - avatar
+ 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.
5th Aug 2017, 4:18 AM
jayStar8705
jayStar8705 - avatar
+ 10
Thanks for your answers guys 😊 really appreciate it
4th Aug 2017, 7:07 AM
Jihad Naji
Jihad Naji - avatar
+ 10
@Garikai Thanks for the explanation ☺
4th Aug 2017, 5:54 PM
Jihad Naji
Jihad Naji - avatar
+ 10
Thank u guys for answering my question ☺ Seriously SL is a great Community 💛
5th Aug 2017, 5:15 PM
Jihad Naji
Jihad Naji - avatar
+ 9
What is good @Aiman Naji? 🤔
4th Aug 2017, 7:12 AM
Jihad Naji
Jihad Naji - avatar
+ 9
@Kirk Schafer thanks alot for the effort of making the code 😊
5th Aug 2017, 8:52 PM
Jihad Naji
Jihad Naji - avatar
+ 7
@Hy You yup it helped 😊 thanks for the explanation
4th Aug 2017, 10:40 PM
Jihad Naji
Jihad Naji - avatar
+ 7
@Jenny thanks for the answer
5th Aug 2017, 9:14 AM
Jihad Naji
Jihad Naji - avatar
+ 7
@Damien do you know what the question really means?
5th Aug 2017, 12:29 PM
Jihad Naji
Jihad Naji - avatar
+ 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.
3rd Aug 2017, 1:21 PM
Hassie
Hassie - avatar
+ 6
There's a good community here, Thanks, guys.
3rd Aug 2017, 2:00 PM
kamil Bolka
kamil Bolka - avatar
+ 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
4th Aug 2017, 12:50 AM
Kirk Schafer
Kirk Schafer - avatar