Runtime Errors

30 Sep, 2020
A runtime error in a program is an error that occurs while the program is running after being successfully compiled.
Runtime errors are commonly called referred to as “bugs” and are often found during the debugging process before the software is released.
When runtime errors occur after a program has been distributed to the public, developers often release patches, or small updates designed to fix the errors.
Anyone can find the list of issues that they might face if they are a beginner in this article.
While solving problems on online platforms, many run time errors can be faced, which are not clearly specified in the message that comes with them. There are a variety of runtime errors that occur such as logical errors, Input/Output errors, undefined object errors, division by zero errors, and many more.
SIGFPE: SIGFPE is a floating-point error. It is virtually always caused by a division by 0. There can be mainly three main causes of SIGFPE error described as follows:Division by Zero.Modulo Operation by Zero.Integer Overflow.Below is the program to illustrate the SIGFPE error:C++C++// C++ program to illustrate// the SIGFPE error  #include <iostream>using namespace std;  // Driver Codeint main(){      int a = 5;      // Division by Zero    cout << a / 0;    return 0;}Output:
Division by Zero.Modulo Operation by Zero.Integer Overflow.
Division by Zero.
Modulo Operation by Zero.
Integer Overflow.
Below is the program to illustrate the SIGFPE error:
C++
// C++ program to illustrate// the SIGFPE error  #include <iostream>using namespace std;  // Driver Codeint main(){      int a = 5;      // Division by Zero    cout << a / 0;    return 0;}
Output:
SIGABRT: It is an error itself is detected by the program then this signal is generated using call to abort() function. This signal is also used by standard library to report an internal error. assert() function in C++ also uses abort() to generate this signal.Below is the program to illustrate the SIGBRT error:C++C++// C++ program to illustrate// the SIGBRT error  #include <iostream>using namespace std;  // Driver Codeint main(){    // Assigning excessive memory    int a = 100000000000;    int* arr = new int[a];      return 0;}Output:
Below is the program to illustrate the SIGBRT error:
C++
// C++ program to illustrate// the SIGBRT error  #include <iostream>using namespace std;  // Driver Codeint main(){    // Assigning excessive memory    int a = 100000000000;    int* arr = new int[a];      return 0;}
Output:
NZEC: This error denotes “Non-Zero Exit Code”. For C users, this error will be generated if the main() method does not have a return 0 statement. Java/C++ users could generate this error if they throw an exception. Below are the possible reasons of getting NZEC error:Infinite Recursion or if you run out of stack memory.Negative array index is accessed.ArrayIndexOutOfBounds Exception.StringIndexOutOfBounds Exception.Below is the program to illustrate the NZEC error:PythonPython# Python program to illustrate# the NZEC Error   # Driver Codeif __name__ == "__main__":      arr = [1, 2]       # Runtime Error    # Array Index out of Bounds    print(arr[2])Output:
Infinite Recursion or if you run out of stack memory.Negative array index is accessed.ArrayIndexOutOfBounds Exception.StringIndexOutOfBounds Exception.
Infinite Recursion or if you run out of stack memory.
Negative array index is accessed.
ArrayIndexOutOfBounds Exception.
StringIndexOutOfBounds Exception.
Below is the program to illustrate the NZEC error:
Python
# Python program to illustrate# the NZEC Error   # Driver Codeif __name__ == "__main__":      arr = [1, 2]       # Runtime Error    # Array Index out of Bounds    print(arr[2])
Output:
SIGSEGV: This error is the most common error and is known as “Segmentation Fault“. It is generated when the program tries to access a memory that is not allowed to access or attempts to access a memory location in a way that is not allowed. List of some of the common reasons for segmentation faults are:Accessing an array out of bounds.Dereferencing NULL pointers.Dereferencing freed memory.Dereferencing uninitialized pointers.Incorrect use of the “&” (address of) and “*”(dereferencing) operators.Improper formatting specifiers in printf and scanf statements.Stack overflow.Writing to read-only memory.Below is the program to illustrate the SIGSEGV error:C++C++// C++ program to illustrate// the SIGSEGV error#include <bits/stdc++.h>using namespace std;  // Function with infinite// Recursionvoid infiniteRecur(int a){    return infiniteRecur(a);}  // Driver Codeint main(){      // Infinite Recursion    infiniteRecur(5);}Output:
Accessing an array out of bounds.Dereferencing NULL pointers.Dereferencing freed memory.Dereferencing uninitialized pointers.Incorrect use of the “&” (address of) and “*”(dereferencing) operators.Improper formatting specifiers in printf and scanf statements.Stack overflow.Writing to read-only memory.
Accessing an array out of bounds.
Dereferencing NULL pointers.
Dereferencing freed memory.
Dereferencing uninitialized pointers.
Incorrect use of the “&” (address of) and “*”(dereferencing) operators.
Improper formatting specifiers in printf and scanf statements.
Stack overflow.
Writing to read-only memory.
Below is the program to illustrate the SIGSEGV error:
C++
// C++ program to illustrate// the SIGSEGV error#include <bits/stdc++.h>using namespace std;  // Function with infinite// Recursionvoid infiniteRecur(int a){    return infiniteRecur(a);}  // Driver Codeint main(){      // Infinite Recursion    infiniteRecur(5);}
Output:
Avoid using variables that have not been initialized. These may be set to 0 on your system but not on the coding platform.
Check every single occurrence of an array element and ensure that it is not out of bounds.
Avoid declaring too much memory. Check for the memory limit specified in the question.
Avoid declaring too much Stack Memory. Large arrays should be declared globally outside the function.
Use return as the end statement.
Avoid referencing free memory or null pointers.
Articles
Competitive Programming
Difference Between
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.