Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a program to handle exceptions using setjmp and longjmp for error handling in C.

Hard

Problem Statement

Write a program to handle exceptions using setjmp and longjmp for error handling in C.

Sample Input

dividend = 10, divisor = 0

Sample Output

Error: Division by zero

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
#include <stdio.h> #include <setjmp.h> jmp_buf buffer; void divide(int dividend, int divisor) { if (divisor == 0) { longjmp(buffer, 1); } else { printf("Result: %d\n", dividend / divisor); } } int main() { if (setjmp(buffer)) { printf("Error: Division by zero\n"); } else { divide(10, 0); } return 0; }
Log in or sign up to run or submit your code.Log inSign up
Input
dividend = 10, divisor = 0
Expected Output
Error: Division by zero