logo

Runtime Errors

Runtime Errors:

  • Ett körtidsfel i ett program är ett fel som uppstår när programmet körs efter att ha kompilerats.
  • Runtime errors kallas vanligtvis för buggar och hittas ofta under felsökningsprocessen innan programvaran släpps.
  • När körtidsfel uppstår efter att ett program har distribuerats till allmänheten släpper utvecklare ofta patchar eller små uppdateringar som är utformade för att åtgärda felen.
  • Vem som helst kan hitta listan över problem som de kan möta om de är nybörjare Denna artikel .
  • När du löser problem på onlineplattformar kan många körtidsfel uppstå, som inte är tydligt specificerade i meddelandet som följer med dem. Det finns en mängd olika körtidsfel som uppstår som t.ex logiska fel , Inmatnings-/utgångsfel , odefinierade objektfel , division med noll fel , och många fler.

Typer av körtidsfel:

  • SIGFPE: SIGFPE är en flytpunkt fel. Det orsakas nästan alltid av en division med 0 . Det kan huvudsakligen finnas tre huvudorsaker till SIGFPE-fel som beskrivs enligt följande:
    1. Dividera med noll.
    2. Modulo Operation by Zero.
    3. Heltals överflöde.
    Nedan är programmet för att illustrera SIGFPE-felet:C++
    // C++ program to illustrate // the SIGFPE error #include  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.');  }  } }>
    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()>
    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);>
    Produktion:
  • SIGABRT: Det är ett fel i sig som upptäcks av programmet då denna signal genereras med anrop till avbryta() fungera. Denna signal används också av standardbiblioteket för att rapportera ett internt fel. hävda() fungera i C++ använder också abort() för att generera denna signal. Nedan är programmet för att illustrera SIGBRT-felet:C++
    // C++ program to illustrate // the SIGBRT error #include  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>
    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()>
    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();>
    Produktion:
  • NZEC: Detta fel anger Utgångskod som inte är noll . För C användare, kommer detta fel att genereras om main() metod har ingen avkastning 0 påstående. Java /C++-användare kan generera detta fel om de skapar ett undantag. Nedan är de möjliga orsakerna till att få NZEC-fel:
    1. Oändlig rekursion eller om du får slut på stackminne.
    2. Negativt arrayindex nås.
    3. Undantag för ArrayIndexOutOfBounds.
    4. StringIndexOutOfBounds undantag.
    Nedan är programmet för att illustrera NZEC-felet:C++
    #include  #include  using namespace std; int main() {  vector arr = {1, 2};  prova { // Avsiktligt fel: Åtkomst till ett 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>
    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.>
    Pytonorm
    # 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
    // 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]);>
    Produktion:
  • SIGSEGV: Detta fel är det vanligaste felet och kallas Segmenteringsfel . Det genereras när programmet försöker komma åt ett minne som inte har tillåtelse att komma åt eller försöker komma åt en minnesplats på ett sätt som inte är tillåtet. Lista över några av de vanligaste orsakerna till segmenteringsfel är:
    1. Åtkomst till en array utanför gränserna.
    2. Avlägsna NULL-pekare.
    3. Avlägsna frigjort minne.
    4. Avlägsna oinitierade pekare.
    5. Felaktig användning av & (adress till) och * operatörer.
    6. Felaktiga formateringsspecifikationer i printf- och scanf-satser.
    7. Stack overflow.
    8. Skriver till skrivskyddat minne.
    Nedan är programmet för att illustrera SIGSEGV-felet:C++
    // C++ program to illustrate // the SIGSEGV error #include  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.>
    Pytonorm
    # 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);  } }>
    JavaScript
    // 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.>
    Produktion:

Sätt att undvika Runtime Errors:

  • Undvik att använda variabler som inte har initierats. Dessa kan vara inställda på 0 på ditt system men inte på kodningsplattformen.
  • Kontrollera varje enskild förekomst av ett arrayelement och se till att det inte är utanför gränserna.
  • Undvik att deklarera för mycket minne. Kontrollera minnesgränsen som anges i frågan.
  • Undvik att deklarera för mycket Stapla minne . Stora arrayer bör deklareras globalt utanför funktionen.
  • Använd retur som slutsats.
  • Undvik att referera ledigt minne eller noll-pekare .