Win a copy of Kotlin Cookbook this week in the Kotlin forum!

Eric Kaiser

+ Follow
since Jan 07, 2020
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Eric Kaiser

Campbell Ritchie wrote:Yes, I can see the problem now; thank you for explaining it. You are lucky that you are unlikely to repeat that mistake.

You are welcome . Yeah for sure. I thank HeadFirst Java for making these basic concepts so clear.
1 day ago

Campbell Ritchie wrote:Well done

But what was the problem?

You see in the main function I have taken a do-while loop. In that do-while loop I am creating an object of type LinkedList every time the loop gets executed. So the head node in LinkedList class is getting created for each object of LinkedList type, though the reference variable ll remains the same.

Now I can either take this line out of the loop or can make the head node static. That did the trick
1 day ago
Solved the issue. Pretty silly one.
1 day ago
Hi everyone!

I am learning Singly Linked List implementation in Java. So in the below code whenever I am inserting a node and trying to display the nodes, none of the nodes are getting printed. I checked that my head node is always remaining null. I can't understand why. Maybe I am making a silly mistake somewhere but really can't figure it out. Any help will be appreciated.

Thank you.

1 day ago
Thank you everyone! Noted down all the points in my head. Thanks again guys.
2 weeks ago
Thanks to Campbell Ritchie and Junilu Lacar! I understood why and what is happening in all the codes I posted.

That would be the wrong conclusion. Go through the tutorials and understand what each kind of exception (checked vs. unchecked) is for. It's quite appropriate to allow unchecked exceptions to blow up and interrupt program execution while you're still developing and testing your program. In this case, you want that to happen because it allows you to find developer mistakes and fix them. As for checked exceptions, it may be appropriate to allow them to bubble up to a level where the decision on how to handle them makes sense. It all depends on the context.

I don't know how to tag an user but this question is for Junilu Lacar or to anyone who can answer this. I get that unchecked exceptions are useful for the developer and also those exceptions usually happen when there is a mistake done by the developer in programming logic. But for checked exceptions why will you like to allow it? I mean we have to handle them in a catch block regardless or have to declare it. Unless I am debugging an application during development I don't think I will like to have checked exceptions arise as ultimately they won't allow to get the code compiled.

2 weeks ago

Campbell Ritchie wrote:That is an unchecked exception. The compiler will allow it to compile without needing a catch, and the finally will be executed. I suggest you go through this part of the Java™ Tutorials.

Ok so if I get this right, if an exception occurs I need to handle it with the catch() except if it is a unchecked exception that gets a free pass from the compiler. Ultimately I conclude that whatever the exception maybe it is best to handle the exception with the catch() block.
2 weeks ago

Junilu Lacar wrote:

Eric Kaiser wrote:"If you define a try block, you can pair it with a matching catch or finally block, or both."

So by this I understand you don't have to handle the exception if it arises or not, finally() block will always run.

It's not a question of whether or not the finally block will run. What you got there was a compile-time error because of the rule that checked exceptions must be either declared with throws or caught in a catch() block. Since you did neither, then the code will not compile. The presence or absence of a finally block is irrelevant.

I see. While reading on this try/final block topic I stumbled upon this piece of code online:

This code got executed without any issues but I did not expect this to. Why?

The output is given as:

Executing finally block
Exception in thread "main" java.lang.NullPointerException
at Main.print(
at Main.main(

Now this code differs from mine which did not execute in the case that here is no method which mentions an exception is being thrown of any kind for example:

But when I executed code with this change still the output remained same. Why?
2 weeks ago

Dave Tolls wrote:MyEx extends Exception, which means that it needs to be handled (as the compiler error says).
It can be handled either by catching it in a catch block, or by changing the method signature to show that it throws the exception.

Your second code works because you have told Java that the exception is being passed out of the method rather than being handled inside the method, by changing the signature to note the exception thrown.

But the statement said: "If you define a try block, you can pair it with a matching catch or finally block, or both."

So by this I understand you don't have to handle the exception if it arises or not, finally() block will always run.
2 weeks ago
Hi everyone! Newbie here.

I am learning Java from Head First Java. I have completed upto exceptions. There is a "Code Magnets" exercise on page 349. After solving it I decided to have some fun with the code.

There is a statement in the chapter: "If you define a try block, you can pair it with a matching catch or finally block, or both." So this statement says a try/final block without the catch block will also work.

So I took the code from "Code Magnets" and pasted it in an online Java editor to test the above statement. The code normally goes like this:

Here the output will be if input is yes: thaws and if input is suppose no then output is throws

Now my code where I decided to test the above statement.

But to my surprise the code did not execute. I got an error: error: unreported exception MyEx; must be caught or declared to be thrown

Now I have read that by declaring an exception I can duck it. So I modified the code as:

Now the output is given as:

Exception in thread "main" MyEx
at Main.doRisky(
at Main.main(

So finally it works as usual.

Can any kind soul explain to me why the previous code of mine did not work but the second one worked? According to me both codes should work as the statement said.
2 weeks ago