Open In App

Runtime Errors

Improve
Improve
Like Article
Like
Save
Share
Report

Runtime Errors:

  • 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:
    1. Division by Zero.
    2. Modulo Operation by Zero.
    3. 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 Code
    int main()
    {
    
        int a = 5;
    
        // Division by Zero
        cout << a / 0;
        return 0;
    }
    
    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.");
            }
        }
    }
    
    C#
    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
    // JavaScript program to demonstrate division by zero behavior
    
    // Perform division by zero
    let result = 5 / 0;
    
    // Print the result
    console.log(result);
    
    Python3
    # 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()
    
    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++ 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;
    }
    
    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
    
    JavaScript
    // 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();
    
    Python3
    # 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()
    
    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:
    1. Infinite Recursion or if you run out of stack memory.
    2. Negative array index is accessed.
    3. ArrayIndexOutOfBounds Exception.
    4. StringIndexOutOfBounds Exception.
    Below is the program to illustrate the NZEC error: Python
    # 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])
    
    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:
    1. Accessing an array out of bounds.
    2. Dereferencing NULL pointers.
    3. Dereferencing freed memory.
    4. Dereferencing uninitialized pointers.
    5. Incorrect use of the “&” (address of) and “*”(dereferencing) operators.
    6. Improper formatting specifiers in printf and scanf statements.
    7. Stack overflow.
    8. 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
    // Recursion
    void infiniteRecur(int a)
    {
        return infiniteRecur(a);
    }
    
    // Driver Code
    int main()
    {
    
        // Infinite Recursion
        infiniteRecur(5);
    }
    
    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
    # 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.
    
    C#
    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);
        }
    }
    
    Output:

Ways to avoid Runtime Errors:

  • 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.


Last Updated : 28 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads