This establishes a stacktrace at the line " throw new exception", and a "caused by" the original exception with it's own stacktrace. Might be useful in certain circumstances. It also changes the class of the exception being throw; for example the catch would catch a RuntimeException (which is unchecked), but it throws an Exception which is checked, forcing a different behavior on the calling routine.
This is not necessarily bad code. However it warrants a comment explaining why it is done.
(And the final is not necessary, although is true. It is fashionable to label everything that is final, if it is so, because of the Functional Programming fashion.)
Yup, if anything, this catch clause is hindering the app's maintainability. And in general, catching any Exception is weird, as it means you know exactly everything that may go wrong in your method. And, if you actually do, why not fix whatever you can beforehand ?
Catching an exception at the top level to show a friendly error is obviously not a problem, but I don't think that's what you meant.
You do not always know what will go wrong, but you can know the types of things that can go wrong.
A connection could time out. A system on the other end of that connection could respond with nonsense. The user could enter data that is formatted correctly but is actually garbage. An entire non-time-critical process can fail, and as long as you catch the error and log it for later resolution, it's no real cause for concern.
There are countless things that can go wrong that don't necessarily need to stop your program from continuing.
This code basically catches every exception and wraps it in another Exception, which will add a new Element to the Stacktrace. e.g. if this code catches an IOException, the Stacktrace would look something like this:
java.lang.Exception
//Stacktrace until the throw of new Exception(e)
caused by: java.lang.IOException "Text"
//original Stacktrace
If you don't write any try catch block and just add a
throws Exception
Clause to the method declaration, you get basically the same effect. This says, that the method can throw Exceptions of type Exception and any Exceptions thrown inside the function will just propagate out, no need to catch and rethrow it.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/ILikeToSwimNaked, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
For most clean text, OCR is very good, but it isn't yet at a stage where it's reliable enough to not at least be double-checked by a human, and its work also needs to be formatted into our appropriate template. Many of us do use its work as a starting point.
However, code is actually the thing our bot has the most trouble with - we use ocr.space's API, but I'm assuming the trouble is that it's trained on non-monospace fonts, as I almost always does terribly with code. The fact that this is a photograph and not a clear screenshot probably wouldn't have helped.
final here just means const. It prevents you from future assignment e = somethingElse. Sometimes it allows the compiler to do some marginal optimization, plus it's considered good practice. sometimes you'll see entire codebases where every variable and parameter is marked final.
This is indeed just a myth. Starting from Java 8, variables that aren't reassigned are consigned by the compiler effectively final and will get the same optimizations. You should use the final keyword only when declaring contstants or variables that actually are not meant to be reassigned. Spamming final everywhere is just pointless and verbose
That seems kind of pedantic. If something like this actually makes a difference then do what JavaScript does and just introduce var/let/const instead. It seems like a silly code style to have marginal performance increases, which if you're caring that much about shaving off cycles here and there then you may as well just do it all in a non-managed language where you can ultimately control it all yourself.
It is generally a good practice to label everything that's final (or const for the non-Javas of the C world) as final, in this case it just prevents reassignment, doubt it'd affect performance or anything.
This is indeed just a myth. Starting from Java 8, variables that aren't reassigned are consigned by the compiler effectively final and will get the same optimizations. You should use the final keyword only when declaring contstants or variables that actually are not meant to be reassigned. Spamming final everywhere is just pointless and verbose
In java 7 and below, if you needed to reference it from an anonymous class in the catch block, you would have had to explicitly mark the variable as final.
Pretty common (atleast in php land) to catch one type of error to throw one that is generally handled, although that is not what appears to be happening here.
This would have made much more sense if it was throw new RuntimeException(e), because I sometimes do that if something throws a checked exception, but I neither want to handle it right here nor want to/can add a throws clause.
My lecturer told us that in Java specifically you should always catch your potential exceptions, even if all you do in the catch block is throw them down another level
Uff. I do something like this but I have my own Exception classes that sent me an E-Mail via NLog about the exception, stacktrace and the NDC, so I always know when something crashes badly and I have to react. So I at least have a reason for it. And with the log framework, most of the time I not even have to programm a message box because I did that once as an NLog addon and its configured to popup for fatal logging.
I get 10 mails per year btw. Most of the time, it's bad user input. I call it the ShIShO principle.
Different teams have different style guides, and whilst the default convention in Java is the braces on same line, it doesn't mean everyone has to follow that.
They could be a C# programmer for instance, and used to braces on new line being the norm.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/ILikeToSwimNaked, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
6
u/[deleted] Jul 17 '21
Why is this even merged should not have passed the PR
Unless it’s a school work or something else
78
u/[deleted] Jul 16 '21
This establishes a stacktrace at the line " throw new exception", and a "caused by" the original exception with it's own stacktrace. Might be useful in certain circumstances. It also changes the class of the exception being throw; for example the catch would catch a RuntimeException (which is unchecked), but it throws an Exception which is checked, forcing a different behavior on the calling routine.
This is not necessarily bad code. However it warrants a comment explaining why it is done.
(And the final is not necessary, although is true. It is fashionable to label everything that is final, if it is so, because of the Functional Programming fashion.)
199
u/knightwhosaysnil Jul 16 '21
it's a security feature! this way nobody knows specifically why your app is so bad
62
u/Capitalist_Kerbal Jul 16 '21
No, no, we like our stack traces long and tedious, like our spaghetti code.
13
u/[deleted] Jul 16 '21
Just use:
and let everyone suffer
39
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 16 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
10
u/RyanNerd shameless Jul 16 '21
Reading code like this gives me migraines.
3
u/HanS_CZE Jul 16 '21
This got me thinking, is there actually any reason to ever catch an exception as final?
7
u/0xF013 Jul 16 '21
Maybe it’s a wrapper to downgrade checked exceptions to unchecked?
2
u/jexmex Jul 17 '21
Pretty common (atleast in php land) to catch one type of error to throw one that is generally handled, although that is not what appears to be happening here.
1
u/you0are0rank Jul 17 '21
That's just not handling exceptions with more steps
1
u/CopperyMarrow15 Jul 17 '21
Get a few of them going and it's called juggling.
1
u/Korzag Jul 17 '21
I'm no Java expert but I'd like to think the Java compiler is smart enough to optimize this out.
1
u/Cybear_Tron Jul 17 '21
Its even funnier the second time!
1
u/Yoshalina Jul 17 '21
This would have made much more sense if it was throw new RuntimeException(e), because I sometimes do that if something throws a checked exception, but I neither want to handle it right here nor want to/can add a throws clause.
1
u/slipped_and_missed_x Jul 17 '21
My lecturer told us that in Java specifically you should always catch your potential exceptions, even if all you do in the catch block is throw them down another level
1
u/mattmc318 Jul 17 '21
Ngl, I've done something exactly like this in Python
1
u/Lord_Pinhead Jul 18 '21
Uff. I do something like this but I have my own Exception classes that sent me an E-Mail via NLog about the exception, stacktrace and the NDC, so I always know when something crashes badly and I have to react. So I at least have a reason for it. And with the log framework, most of the time I not even have to programm a message box because I did that once as an NLog addon and its configured to popup for fatal logging.
I get 10 mails per year btw. Most of the time, it's bad user input. I call it the ShIShO principle.
1
u/IGotSkills Jul 19 '21
hey!!!! I once built a linked list out of exception handling. it worked great.
-10
u/Shnorkylutyun Jul 16 '21
You can already see from where the braces are - some noob at work
0
u/[deleted] Jul 16 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 16 '21
[deleted] — view deleted comment