Learning Programming #2.2 Learning Java: Exceptions

Besides compiler errors which your learned about in my last post there are other 'errors' in java called exceptions.

Exceptions are "thrown" every time your program tries do to something that shouldn't be done.

For example if you try to access the 100th element of an array of the size 20 you will get an ArrayIndexOutOfBoundsException.

Other sources of exceptions are for example accessing variable of objects that haven't been assigned(i.e. that are null) you will get a NullPointerException,
or when you try to access a file that is currently being modified you get an IOException, …

Exception are always thrown at runtime because the compiler cannot keep track of every possible outcome of for example you trying to access an array.

Exceptions usually stop the program, unless they are catched. You can catch them with:

try {
    // Put the code that throws an Exception here:
}
catch(Exception someException) { // You can use other types of exceptions here like ArrayIndexOutOfBoundsException.
    // Code that is put here will be executed if the exception is thrown.
    // You don't need to put anything here.
    // If you want to see the contents of the exception you should do:
    someException.printStackTrace();
}

The Exception-catching above should be used with caution as it can disguise bugs inside your program. Do not use a try-catch if you don't know what you are doing! Don't use it if you are to lazy to fix the exception inside your code! This will lead to bugs in your program.

When an exception is thrown you will get printed a lot of information. I will explain how to resolve exceptions with a small example. Consider the following code. It gives the exception shown below:
Screenshot from 2019-06-02 10-13-15.png
If you are wondering why 2 is already out of bounds for an array of length 2 you should take a look at #1.5.
The exception name and description already tells us that somewhere in the program the index "2" of an array of length "2" is accessed, although it shouldn't be.
The structure below tells us where the exception happened(it happened in line 5 of the function getSomeValue() in the class ExceptionTest). The lines below tell us from which function the function in the previous line was called. In this case getSomeValue() was called in line 11, in addArrayValues(), which was called in line 18 in main().

If you call functions of other objects with some invalid arguments, the first function mentioned won't be inside your program, so you will need to scroll down the exception data to get data fix the invalid arguments you gave.

Task

Fix the exception by changing one character in my program.

Sort:  

Honestly I have no idea what you're talking about here 😂, but @bengy featured you in our pay it forward curation contest (@pifc)! I really wish I did understand this stuff though 😀

I really wish I did understand this stuff though 😀

I made some more posts during the last months trying to explain the basics of programming:
#0, #1.1, #1.2, #1.3, #1.4, #1.5, #1.6
I tried to make them as beginner-friendly as I can, so maybe this will help you. If you get stuck anywhere you can always ask.

Thanks for you reply.

Thank you!

Oh man... it has been waaaaaaayyyy too long since I tried my hand at programming. I remember endless nights at university trying to figure out bugs in my code... I hated it so much... even more so as I was a Physics student, and I had friends who were CompSci, and they had elegant code... and I had horrible spaghetti crap that didn't work!

I should sit down and try and relearn this stuff when I have more time!

I have featured you and this post for a Curation Contest here:

https://steemit.com/curation/@bengy/payitforward-week61entry-n5bdjcemi2

You can join the fun here:

https://steemit.com/payitforward/@pifc/week-61-pay-it-forward-curation-contest

trying to figure out bugs in my code

Yes that always takes some time… And in the end you find out that only one letter(one time a 'K' instead of a 'k' took me a whole weekend) was wrong. :(

But if you don't let others (like the CompSci's in your case) discourage you and get more practice(I suggest games for that one), debugging won't be needed as often and gets easier.

horrible spaghetti crap

It isn't important how your code looks like, as long as you understand it.

I should sit down and try and relearn this stuff when I have more time!

You really should. Especially as a physicist it can be very useful to get more comfortable with programming.

I will give a go... By games, do mean programming themed games or actually programming a game? I once did an absolutely horrific Tetris...

What do you mean by "themed game or actual game"?

It actually doesn't really matter what you program, but it matters that you program(You could also create a program that numerically solves the schrödinger equation if you want).
I just suggest games, because you can have a lot of fun programming and testing them, and fun is beneficial in learning.

The best way is taking a simple game, that is easy to play and follows simple rules. Yes Tetris is a good example, although I'm not sure if the collision detection and turning of those weird shapes isn't a bit hard to begin with.
I'd also recommend something like snake or that chrome-browser-dino-game.
Here you should not decide based on what others (like me) tell you, but on what you would like to play.
You can also try to reach for higher goals. With the right degree of simplification(like reducing 3d to 2d graphics or make far less objects/obstacles or simplify an AI to a simple mathematical function or making only square boxes in tetris) you could program a small version of every game there is, even as a beginner.
→ So just take on of your favourite computer games from your childhood and go ahead and program a (heavily) reduced version of it.

Lol. I haven't done much programming, though I do hand-code HTML - and am trying to figure out ways to make Markdown do what I want it to do (sometimes successfully, sometimes not.)
Anyway, I like that you're doing this series. It's a great post.

I found you because @bengy featured you in the Pay it Forward Curation Contest. Keep up the great work!

Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 50 comments. Your next target is to reach 100 comments.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

You can upvote this notification to help all Steem users. Learn how here!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 65012.58
ETH 3101.28
USDT 1.00
SBD 3.86