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:
- Dividera med noll.
- Modulo Operation by Zero.
- Heltals överflöde.
// 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; }>
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);>
- 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; }>
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();>
- 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:
- Oändlig rekursion eller om du får slut på stackminne.
- Negativt arrayindex nås.
- Undantag för ArrayIndexOutOfBounds.
- StringIndexOutOfBounds undantag.
#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> 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]);>
- 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:
- Åtkomst till en array utanför gränserna.
- Avlägsna NULL-pekare.
- Avlägsna frigjort minne.
- Avlägsna oinitierade pekare.
- Felaktig användning av & (adress till) och * operatörer.
- Felaktiga formateringsspecifikationer i printf- och scanf-satser.
- Stack overflow.
- Skriver till skrivskyddat minne.
// 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); }>
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.>
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 .