Although the task is passed, the same code doesn't work in IntelliJ. It shows that block try is wrong.
I send my code in the comments
Dyskutowane
Komentarze (11)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Magda
15 marca, 17:11
Now it works, thanks
+1
Magda
11 marca, 11:02
The only thing I changed in the code was adding a try-catch block. The types of exceptions that programme uses was already given so I didn't check if their type is appropriate.
I was wondering why the system isn't able to find the symbols of the methods. => As a result the whole method isn't working.
+1
Thomas
11 marca, 13:07przydatny
In the English version this task (0917) looks different (I looked it up now). The three methods throw Runtime exceptions (as expected - that you not necessarily have to catch).
The easiest way to solve it is to move the three method calls into a try block and to catch all Exceptions
So something probably went wrong when moving that task to Polish?? Here you'd have to import the IO- and RemoteException (if that hasn't been done) and the try-catch is of course mandatory here (or as you now know you add to the signature of the methods main and obslugaExceptions that they throw these exceptions - but that wouldn't meet the task requirements, just be valid code) +1
Thomas
11 marca, 13:10przydatny
your code fixed... no clue if that'll validate as it is differs to much from the original (and the requirements that I can read)
+1
Magda
9 marca, 19:08
Understood :)
+1
Magda
9 marca, 17:33
So thank you, Thomas, for explaining me types of exceptions and catching them. You complained that I don't give you feedbacks.
I usually mark your comments as "helpful" and take notes of what you've written. It's because one explanation is in most cases enough for me. As I seldom have more questions, I though that comments like "Thanks so much :)" would be useless since I can just add a bookmark to your response. I didn't know you can't see them.
I really appreciate that you anwered so many of my questions and were waiting for my feedback. I couldn't ask for more :)
+1
Thomas
9 marca, 18:03
Sometimes I directly ask you questions and you never answer.
Like here for task0917... no answer so far
or https://codegym.cc/pl/help/17670
or https://codegym.cc/pl/help/17754
or https://codegym.cc/pl/help/17702
(Don't worry, a response to those is no longer necessary)
I don't expect thank-you notes for every little bit of help I give but when I write exhaustive answers or write code for you, even with possibilities to chose from then I'd love to hear if it was helpful or which path you've chosen.
Talking against a wall isn't that funny.
+1
Magda
8 marca, 18:54
public static void main(String[] args) {
obslugaExceptions(new Solution());
}
public static void obslugaExceptions(Solution obj) {
obj.method1();
obj.method2();
obj.method3();
}
public void method1() throws IOException {
throw new IOException();
}
public void method2() throws NoSuchFieldException {
throw new NoSuchFieldException();
}
public void method3() throws RemoteException {
throw new RemoteException();
}
0
Thomas
9 marca, 09:45przydatny
Did you modify the given code? The signatures of the methods throwing exceptions and the type of exception they throw? task0917 is about unchecked exceptions. These methods throw checked exceptions.
In short:
checked exception: when something outside the control of the program/ coder leads to a problem you / the writer of a method uses a checked exception. It needs exception handling. All checked exceptions inherit Exception
unchecked exception: when there's a programming error occurs, something you as programmer have in your control. These exceptions do not need exception handling but you can if you want. All unchecked exceptions inherit RuntimeException (take care, RuntimeException inherits Exception, too).
These distinctions are not strict.
Exception handling for checked exceptions:
As said the need handling. You do that in a try-catch block.
If somewhere in your code an exception is thrown
you have two options.
1. Directly handle it using try-catch
2. You can declare the method in which the exception occurs as such using the throws keyword in the signature. Then the caller of that method needs to handle it using try-catch (or you declare it as a method throwing an exception)
Exception handling for unchecked exceptions:
As said, you can but you mustn't. If you decide to handle unchecked exceptions the above said is valid.
If not the program just ends if you throw one like
+3
Thomas
9 marca, 09:58przydatny
To your program.
methods 1-3 throw checked exceptions. You easily can see that when looking at the popup help in IntelliJ (mouseover the excptions name, wait half a sec and read. It shows all the classes the exception inherits. If one of them is RuntimeException, it is an unchecked exception, if not, a checked exception).
This means something is wrong as the task is about unchecked exceptions. You probably have changed the method signatures and the exception types to be thrown.
Doesn't matter as it is about to catch exceptions. This you have to do here in the obslugaExceptions() method (task condition) and not in the methods the exceptions occur.
For that you have several ways.
1. Catch all exceptions, checked or unchecked
2. a) check just the exceptions that occur
you need take care of the order. If an exceptions inherits from another and you place the more general above in the cascade then it catches the descendant, too (see RemoteException and IOException - RemoteException inherits IOException and has to come before)
2. b) just the exceptions that occur but in one catch block
3. use dedicated catch blocks for each method that can throw an exception +3
Thomas
9 marca, 10:01
Since I never get any feedback from you and I don't know if I'm typing my fingers to the bone for nothing and just wasting my time, this is the last question from you I'll answer.
+2