#Order of exception handling
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
There's something which I was wondering baout exception handling and the order you'Re supposed to put them in : I understand there needs a specific order because of the types of the objects, but how is one supposed to know that object X is a subclass of another class? Going in the source code? Checking some online picture which shows the order of each class for error handling?
Documentation I reckon should suffice in most cases
Your IDE might also show you a warning for dead code
you mean if i put the wrong order it will tell me
Given that I haven't written a lot of java in the past year or two, I'm not entirely sure haha
I've been stuck doing more ops-y things
Platform stuff rather than writing code
you mean on the graphical interface?
Maintaining pipelines and openshift clusters
ok i have no idea what this is
btw random question
WHy is your username Seraphim?
Those are the baby angels right
No, those are cherubim, idk how I came up with the name
I just wanted a name without numbers in them lol
Regardless, this would be easy to test 😛
yes im going to do that jsut now
Catch RuntimeException before any of the many derivatives
yes it give you a warning
Like IllegalArgumentException
wait going to pictuer it
so it seems like there's no other way than checking online for the order by the looks of it
@warm sand Just wanted to confirm though, if I have different objects at the same level, the order here does not matte anymore, right
it's only when there's a risk of having a super class being put before its childs that this becoems a problem
Correct, a class can only inherit from one other class 🙂
wait, no that's not what i meant
As in, two sibling classes can never be interfering with each other
Yes haha
ah ok thanks
Java can check at compile time that a catch block can never be reached if a prior catch block catches all the exceptions that would go into the later one:java try { something(); } catch (Exception e) { // Do something } catch (IOException e) { // This gives a compile-time error. // Do something }
Detected code, here are some useful tools:
yes, thats what i noticed with my error message
Okay sweet.
btw do you have an answer for my original question
Do you mean the original question in this thread?
so basically, i understand this hwole order thing, what i was wondering is how does one know the order, bascially searching for online pictures showing it? source code?
The main way is basically just checking the source code. Typically, methods document what kinds of exceptions they throw, and usually someone can make an "educated guess" and then confirm what they know by checking the source code if they want. For example, FileNotFoundException is a subtype of IOException. Sometimes exceptions are named something blatantly more specific, so it's easy to tell, e.g. ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException, which is fairly simple to see I guess.
Typically the programmer is expected to know the hierarchy though. The javadocs that are generated online are helpful with that (they list the class hierarchy for types), and during compile-time the IDE can usually check and tell you if you try to handle a checked exception that cannot actually be thrown from the try block.
Here's an example of the docs for IndexOutOfBoundsException: https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html
Near the top, it lists the "Direct Known Subclasses" of the type.
oh ok, this makes sense. And like you said, in the very worst case scenario, my compiler will me if there's something wrong
so it's not that bad
Thanks again!
You're welcome. :-)
Closed the thread.
re open
I forgot to ask something : when you say "thrown", you mean the process of creating an object of the type of the class
is my terminology correct here?
Oh, no "thrown" means that an exception was used with the throw operator:java if (userInputIsBad()) { throw new IllegalArgumentException("That input is not valid!"); // This "throws" an IllegalArgumentException. }
Detected code, here are some useful tools:
but this creates an object here in your code no? ( i did not learn about this operator)
The new operator creates a new object, but the throw operator causes the method to stop executing and to finish "with an exception." That exception will "propagate up" the call hierarchy until it is caught by a try-catch block:```java
void doSomething() {
try {
doSomethingElse();
} catch (Exception e) {
// An exception was thrown by doSomethingElse(), but we caught it.
}
}
void doSomethingElse() throws Exception {
throw new Exception("Example");
}```
Detected code, here are some useful tools:
so throw basically means that it stops your method and send you an error message?
It stops the method, but it sends the caller an exception object (which is sort of like a message; it tells the caller what kind of issue occurred).
Exceptions are their own sort of beast. I'd like to find some resource that explains them in detail (there's a lot involved) but I don't know any off the top of my head haha.
It's ok, it's just my curiosity trying ot understand
unfortunaly, i have little time to do everything
Why are you limited on time?
well, in school, you have to advance, regardless of your true understanding of something
Oh, yeah. I understand that.
@autumn field Something which came to my mind : if I have a finally block, and for some reason my program had an exception to it which was no caught by any catch, if I understood correctly, my program will crash, but it will still execute the finally before crashing, right?
Yes, the finally block gets executed after the corresponding try block completes, whether or not the try block completes normally (without throwing an exception, returning, or "jumping") or exceptionally (by throwing an exception, returning, or jumping).java void doSomething() { try { System.out.println("test"); return; } finally { // We get to this part as soon as the try part finishes, no matter how it finishes. System.out.println("This gets executed AFTER the return statement but BEFORE the method finishes."); } }
Detected code, here are some useful tools:
oh there's an implicit "return" statement here (did not learn about those"
anyway, thanks again for your help
you amaze me your knowledge
You're welcome.
And thanks. :)