Saturday, December 28, 2024
Google search engine

Runtime Errors

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;
    }

    
    

    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;
    }

    
    

    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);
    }

    
    

    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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments