I’m going to assume that there’s some exceptions being thrown that implement or extend (idk which) those exceptions and so to make things a bit more consistent it type casts them to those types. Still weird though.
The problem is that this code swallows some exceptions which could contain valuable information, and then throw a new exception not connected to the original one.
I'm guessing this code is expected to raise a limited number of exceptions. If so, these checks may have been added to constrain them as its internal logic became more complicated. If it were to fire off a new exception type that could create back compatibility issues.
Yes and also the jdk has the source bundled as a zip file, which is automatically associated with your ide
So you go to your project, libraries, expand the jdk, and you can look through all of the packages and open the class files directly. You can do normal things like search, find declarations, etc but you cannot edit the source
I suspect there might be something which throws some pretty broad exceptions. Here, the exceptions that can be raised are narrowed from Throwable down to IOException and FontFormatException.
These would be nicer to handle (the method now throws FontFormatException or IOException, not Throwable) or maybe this is implementing an abstraction which only allows IO and FontFormat exceptions
But what's the point of testing the type and then throwing it casted to the narrowed type?
If we just re-throw the Throwable, now our method signature is: ‘void exec() throws Throwable’
If we ensure the type first, now our method signature is: ‘void exec() throws IOException, FontFormatException’
The latter is more explicit on what can go wrong, giving the caller a better hope of dealing with the exception
If we have Throwable > Generic > Specific and we catch Throwable, test the type and it happens to be of type Specific, casting it to Specific and throwing again will not change how any callers handle the Throwable/can test for instance type.
The thrown type declared by the method changes from from Throwable to Specific
Not to mention the swallowing of the Throwable at the end.
My guess: checked exceptions. The method might say in its signature it can throw FontFormatException and IOException. So it tells you what errors might occur and you can only use it if you handle the errors it might throw.
Solid idea in theory, in practice it leads to nonsense like this. IOException just means nothing here and can't be properly handled.
Nowadays many just throw unchecked runtime exceptions and catch all. At least that was my impression from the few java codebases I've seen
249
u/lets_eat_bees Mar 25 '23
Oh yes. A common pattern of error handling called "let's make the exception worse".
32
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 25 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
22
u/[deleted] Mar 26 '23
The jdk is on GitHub now, so submit a PR and let them know this is silly.
14
u/shizzy0 Mar 25 '23
wtf
10
u/pavilionhp_ learning from the best Mar 25 '23
I’m going to assume that there’s some exceptions being thrown that implement or extend (idk which) those exceptions and so to make things a bit more consistent it type casts them to those types. Still weird though.
32
u/skantanio Mar 25 '23
She problem on my font till I data
6
u/Swaag__ Mar 25 '23
Is that a vscode theme? I really like the colors
5
u/Baardi Mar 26 '23
Will never add any useful info. Might remove useful info. No logging. This is worse than useless
4
u/SGVsbG86KQ PHP Mar 26 '23
They potentially re-throw an OutOfMemoryError as IOException, wtf
1
u/fecal_brunch Mar 26 '23
I'm guessing this code is expected to raise a limited number of exceptions. If so, these checks may have been added to constrain them as its internal logic became more complicated. If it were to fire off a new exception type that could create back compatibility issues.
2
u/Brief-Preference-712 Mar 26 '23
How do you view the source code of java.awt.*
2
u/trehinkarsalt Mar 26 '23
Looks like a merge error
1
u/hem1t Mar 26 '23
It works, don't touch it.
-6
u/D3PSI Mar 26 '23
java world in a nutshell lol
0
u/[deleted] Mar 25 '23
[deleted] — view deleted comment