How to deal with Runtime Errors

Everytime I run my code I used to get runtime error, I am too frustrated with it (Is it because I use lot of arrays ), Can anyone tell me how to reduce runtime errors using debugging ?

Hm, you are using Java… Always when you have array of length N, you can use only indexes 0…N-1. When you try to use negative index or index greater than N-1 you will get ArrayIndexOutOfBoundsException (every exception thrown in CodeChef environment ends with runtime exception result).

Typically coders here are using small trick to prevent this problems - instead of array of length N (f.e. int[] a = new int[N]) they allocate bigger array (int[] a = new int[N + 10]). Personally I do not like that approach, because you simply convert runtime error (RE) to wrong answer (WA), but it may help you to find if it s really this problem.

Very common problem in Java is NullPointerException too.

Both of those exceptions are the programmers bugs and occur because of insufficient testing, so you have to test better (and it’s not easy sometimes).

1 Like