Tutorial - How to debug an NZEC error?

There are a lot of posts with NZEC error. This post is aimed at helping to debug NZEC errors.

  1. NZEC stands for Non Zero Exit Code.
  2. Exit codes are codes return by running program to operating system upon either succesful termination(Exit code 0) or termination due to error(Non zero exit code).
  3. In languages which have exception handling like Java, Python etc we can use exception hadling using try - catch blocks.
  4. NZEC is a runtime error.
  5. It mostly occurs when negative array index is accesed or the program which we have written takes up more space than the allocated memory for our program to run.

Code samples and other useful information

Java:

Use exception handling

try{
    
   // code which you think might throw exception
}catch(Exception t){
  // you got the exception. Now what??
}

submit your code with a try catch block and see if it is an Exception, if we get WA then we conclude that it is an exception, even now if you get an NZEC that means it not an exception it is an Error(NZEC output) then try the code sample below

try{
   	new FIRESC().main1();
	// code which you think might throw exception
}catch(java.lang.Throwable t){
   // you got the exception. Now what??
}

As Throwable class is super class of all error and exception classes in Java we get it caught here by any means. Now try to find the error by replacing statement ā€œjava.lang.Throwable tā€ with different error classes from this link, now for which error you get a WA judgement that is the error throwing NZEC.

Python:

In python Exception class is the super class of all errors and exceptions.So use the below code sample

try:
    #code that may throw an error
except Exception,e:
    pass

Then if we get WA then replace Exception with different Exception names till you get a WA and that is the error causing NZEC. Mostly the errors will be due to Memory access or division by zero.

**C: **

NZEC occurs mainly due to

  1. Infinite Recursion
  2. Incorrect Memory Access

Please contribute for other languages like C and C++, I think assert will help to some extent.

Happy Debugging!

21 Likes

but what can u do in case of C

1 Like

Can you tell how to debug it in pascal?, it too have try and exception block.

nice info . Helped me a lot.

Thanks.

Thanks for this it helps!

Java:

I generally use

try
{

entire main class

} catch(Exception e){}

5 Likes

I got it because I didnā€™t remove the package declaration in java

1 Like

https://www.codechef.com/viewsolution/24942865
This is the link to my solution. This works perfectly in Pycharm but not here. Its showing NZEC. Iā€™ve been trying to rectify it for a long time. Please help me out.

3 Likes

Helped me to identify the NZEC error in PYTHON \m/

Here in c++ I found, you must have to return 0 not any other value only zero (0) and problem solved.

1 Like

May be you are not returning 0 in main function, you must have to return 0 only 0 not any other value :+1:

1 Like

Could you please explain in detail?
Which package and what did you do to resolve it?

lst = []
pos = 1

def call(value):
lhs = 2pos
rhs = (2
pos) + 1
if value == pos:
print(value)
elif pos < value:
print(rhs)
elif pos > value:
print(lhs)

def fn(i_d, value):
if i_d == ā€˜iā€™:
if value not in lst:
lst.append(value)
call(value)
else:
print(ā€˜Element already presentā€™)
elif i_d == ā€˜dā€™:
if lst <= []:
print(ā€˜Empty listā€™)
elif value in lst:
lst.remove(value)
call(value)
else:
print(ā€˜Please enter valid inputā€™)

user_input = int(input())
for numbers in range(0, user_input):
first = input()
second = int(input())
fn(first, second)

Below is my code. I am getting error message as NZEC. Can you please help me out.

I also faced same issue, my code was like below and by removing ā€œ.parallel()ā€, it solved :slight_smile: . But Not sure why though.

int sum =Arrays.stream(r.readLine().split(" "))
							.mapToInt(Integer :: parseInt)
                                                        .parallel()
							.filter(...)
							.sum();

thank you so much for the help

CodeChef: Practical coding for everyone can someone tell me why i am getting nzec error in this code

my code is sucessfully run and producing output in online gdb but in Codechefcomplier it doesā€™nt producce any output. ''even after using try catch

Try this for c:
convert void main() to int main()
and return 0 at the end.

Thanks, @drj_reddy . You solved my problem.

Some other ways to resolve the NZEC Error are: Ensure that an illegal arithmetic operation like dividing by zero is not performed. Using try-except blocks can help resolve NZEC errors most of the time. Test your code and always check for corner cases.

This may help you,
Rachel Gomez