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.
Types of Runtime Errors:
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: Below is the program to illustrate the SIGFPE error: Division by Zero. Modulo Operation by Zero. Integer Overflow. 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: 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: Below is the program to illustrate the NZEC error: Infinite Recursion or if you run out of stack memory. Negative array index is accessed. ArrayIndexOutOfBounds Exception. StringIndexOutOfBounds Exception. 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: Below is the program to illustrate the SIGSEGV error: 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.</p><pre><code class="language-cpp">// C++ program to illustrate
// the SIGFPE error
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int a = 5;
// Division by Zero
cout << a / 0;
return 0;
}
</code></pre><gfg-tab slot="tab">Java</gfg-tab><gfg-panel data-code-lang="java" slot="panel"><pre><code class="language-java">public class Main {
public static void main(String[] args) {
int a = 5;
try {
// Division by Zero
System.out.println(a / 0);
} catch (ArithmeticException e) {
// Handle the ArithmeticException
System.out.println("Error: Division by zero is not allowed.");
}
}
}
# Python program to illustrate
# the ZeroDivisionError
# Driver Code
def main():
a = 5
try:
# Division by Zero
print(a / 0)
except ZeroDivisionError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
using System;
class Program
{
static void Main()
{
int a = 5;
try
{
// Division by Zero
Console.WriteLine(a / 0);
}
catch (DivideByZeroException ex)
{
// Handling DivideByZeroException
Console.WriteLine("Error: " + ex.Message);
}
Console.ReadLine();
}
}
// JavaScript program to demonstrate division by zero behavior
// Perform division by zero
let result = 5 / 0;
// Print the result
console.log(result);

</p><pre><code class="language-cpp">// C++ program to illustrate
// the SIGBRT error
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Assigning excessive memory
int a = 100000000000;
int* arr = new int[a];
return 0;
}
</code></pre><gfg-tab slot="tab">Java</gfg-tab><gfg-panel data-code-lang="java" slot="panel"><pre><code class="language-java">public class Main {
public static void main(String[] args) {
try {
// Assigning excessive memory
int a = 1000000000;
int[] arr = new int[a];
} catch (OutOfMemoryError e) {
// Catch the OutOfMemoryError
System.err.println("Caught OutOfMemoryError: " + e.getMessage());
}
}
}
//This code is contributed by Adarsh
# Python program to illustrate
# the MemoryError
# Driver Code
def main():
try:
# Attempting to allocate excessive memory
a = 100000000000
arr = [0] * a
except MemoryError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
// JavaScript program to illustrate the MemoryError
// Driver Code
function main() {
try {
// Attempting to allocate excessive memory
const a = 100000000000;
const arr = new Array(a).fill(0);
} catch (e) {
console.log("Error: " + e.message);
}
}
main();

</p><pre><code class="language-cpp">#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = {1, 2};
try {
// Intentional Error: Accessing an element out of bounds
cout << arr.at(2) << endl; // Accessing index 2 which is out of bounds
}
catch (const out_of_range& e) {
cout << "Error the index is out of bound"<< endl; // Handle the out_of_range exception
}
return 0;
}
//This code is contrbiuted by Adarsh
</code></pre><gfg-tab slot="tab">Java</gfg-tab><gfg-panel data-code-lang="java" slot="panel"><pre><code class="language-java">public class Main {
public static void main(String[] args) {
int[] arr = {1, 2};
// Intentional Error: Accessing an element out of bounds
System.out.println(arr[2]); // Error: Accessing index 2 which is out of bounds
}
}
//this code is contributed by Utkarsh.
# Python program to illustrate
# the NZEC Error
# Driver Code
if __name__ == "__main__":
arr = [1, 2]
# Runtime Error
# Array Index out of Bounds
print(arr[2])
// JavaScript program to illustrate
// the error similar to NZEC Error
// Driver Code
let arr = [1, 2];
// Runtime Error
// Array Index out of Bounds
console.log(arr[2]);

</p><pre><code class="language-cpp">// C++ program to illustrate
// the SIGSEGV error
#include <bits/stdc++.h>
using namespace std;
// Function with infinite
// Recursion
void infiniteRecur(int a)
{
return infiniteRecur(a);
}
// Driver Code
int main()
{
// Infinite Recursion
infiniteRecur(5);
}
</code></pre><gfg-tab slot="tab">Java</gfg-tab><gfg-panel data-code-lang="java" slot="panel"><pre><code class="language-java">import java.util.*;
public class Main {
// Function with infinite Recursion
static void infiniteRecur(int a) {
// Recursively call the function without a base case
infiniteRecur(a);
}
// Driver Code
public static void main(String[] args) {
// Infinite Recursion
infiniteRecur(5);
}
}
//This code is contributed by Monu.
# Python program to illustrate
# the SIGSEGV error
# Function with infinite
# Recursion
def infiniteRecur(a):
return infiniteRecur(a)
# Driver Code
if __name__ == "__main__":
# Infinite Recursion
infiniteRecur(5)
#This code is contributed by Utkarsh.
using System;
class Program
{
// Function with infinite Recursion
static void InfiniteRecur(int a)
{
// Recursively calling the function
InfiniteRecur(a);
}
// Driver Code
static void Main()
{
// Infinite Recursion
InfiniteRecur(5);
}
}
// Function with infinite Recursion
function infiniteRecur(a) {
// Recursively call the function without a base case
infiniteRecur(a);
}
// Infinite Recursion
infiniteRecur(5);
// Note: JavaScript does not have tail-call optimization,
// so running this code will eventually lead to a maximum call stack size exceeded error.

Ways to avoid Runtime Errors: