SlideShare ist ein Scribd-Unternehmen logo
1 von 35
ALGORITHMS AND COMPUTING ( LAB )
END SEMESTER PRESENTATIONS
GROUP C
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Example Program 1 ( DL 1)
     • Causes of Exception Occurrence
     • Example Program 2 ( DL 2)
     • Example Program 3 ( DL 3)
     • Grading
     • Q/A Session
GROUP MEMBERS:
    • Raza Najam
    • Hassaan Idrees
    • Waleed Raza
    • Syed Ahmed Fuad
    • Tanveer Hussain
    • Hassan Qamar Rana
PRESENTATION CONTENTS
           • Team/Topic Introduction
           • Errors & its Types
 Hassaan   • Exception Handling Basics
           • Occurrence & Handling of an Exception ( Flow diagrams )
    Raza   • Methods to handle an Exception
    Fuad   • Example Program 1 ( DL 1)
 Tanveer   • Causes of Exception Occurrence
 Waleed    • Example Program 2 ( DL 2)
    Raza
           • Example Program 3 ( DL 3)
           • Grading
           • Q/A Session
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Example Program 1 ( DL 1)
     • Causes of Exception Occurrence
     • Example Program 2 ( DL 2)
     • Example Program 3 ( DL 3)
     • Grading
     • Q/A Session
ERRORS AND ITS TYPES

• There are three basic types of errors:
         1- Syntax Error
         2- Semantic Errors
         3- Logical Errors
  These errors are detected by the ‘C’ compiler

• There are also a few kinds of errors in ‘C’ those are not detected by the
compiler.
         4- Run Time Errors
         5- Compile Time Errors
  These error are not identified by the compiler and so they are termed
as “EXCEPTIONS”
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Example Program 1 ( DL 1)
     • Causes of Exception Occurrence
     • Example Program 2 ( DL 2)
     • Example Program 3 ( DL 3)
     • Grading
     • Q/A Session
WHAT IS REALLY AN EXCEPTION?
• An exception is an indication of a problem that occurs during a program’s
 execution.
• Exception is a runtime problem that occurs rarely
 handling an exception allows programs to continue executing as if no
 problem had been encountered.
• Helps terminating the program in a controlled manner rather in an
 unpredictable fashion
• Exception handling enables programmers to create applications that can
 resolve (or handle) exceptions.
DIFFERENCES BETWEEN AN ERROR AND AN EXCEPTION
1- Errors occur at compilation time as well as run time while exceptions
mostly occur at run time.
2- Compile time errors are detected by the compiler while exceptions need
to be predicted by the programmer himself
3- Errors occur frequently while exceptions, as the name suggests occur
seldom
4- Error detection and debugging is easier while exception prediction and
its handling is a bit complex procedure
5- Errors can be removed by removing syntax and logical mistakes while
exception handling needs pre-defined or user defined procedures.
BENEFITS:
• Helps improve a program's fault tolerance.
• Enables the programmer to remove error-handling code from the ‘main
line’ of the program’s execution
• Programmers can decide to handle any exceptions they choose – all
exceptions of a certain type or all exceptions of a group of related types
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Example Program 1 ( DL 1)
     • Causes of Exception Occurrence
     • Example Program 2 ( DL 2)
     • Example Program 3 ( DL 3)
     • Grading
     • Q/A Session
EXCEPTION HANDLING IN C
• C does not provide direct support for error/exception handling.
• By convention, the programmer is expected to develop an algorithm for
an error and exception case free program.
• Intelligent visualization skills and prediction of end-user actions on a
particular event prevents errors from occurring in the first place.
• The programmer is expected to test return values from a function.
Exception handling is designed to:
   Process synchronous errors, which occur when a statement executes.
   Common examples of these errors are:
        1. out-of-range array subscripts
        2. arithmetic overflow
        3. division by zero
        4. invalid function parameters
        5. unsuccessful memory allocation, due to lack of memory
Exception handling is not designed to:
   Process errors associated with asynchronous events fro example:
        1. Disk I/O completions
        2. Network message arrivals
        3. Mouse-clicks and keystrokes
   which occur in parallel with, and independent of, the program’s flow
   control.
HOW TO HANDLE AN EXCEPTION:
-   Ignore the exception
                           (Non – Professional )
-   Abort the program
                           ( Even Worst )
-   Set error indicators
                          ( Beginner’s Approach )
-   Issue an error-message and call exit().
                          ( Non – Feasible )
-   Use setjmp() and longjmp()
                          ( Leading to Professional)
THE CLASSIC ‘C’ APPROACH TO EXCEPTION HANDLING.
• Each function returns a value indicating success or failure.
• Every function must check the return code of every function call it
  makes and take care of errors.
• Exceptions make it easy to separate error handling from the rest of
  the code.
• Exceptions make it easy to separate error handling from the rest of
  the code.
• Intermediate functions can completely ignore errors occurring in
  functions they call, if they can't handle them anyway
Main body
{
Any program…

                   Program Pauses and control is
Exception Occurs   shifted to some other function



Further program


End of   program   Now the user defined function
                   decides what to do with that
                   particular exception
}
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Example Program 1 ( DL 1)
     • Causes of Exception Occurrence
     • Example Program 2 ( DL 2)
     • Example Program 3 ( DL 3)
     • Grading
     • Q/A Session
METHODS OF EXCEPTION HANDLING

There are two methods to handle an exception in C.
1- if - else method
2- setjmp() and longjmp() method
METHODS OF EXCEPTION HANDLING

1- if - else method
     • Exception is handled by making decisions via if – else.
     • Occurrence of an exception is checked by the return
     values of a function or by defined parameters.
     • Along with that condition is placed an if – else statement
     that checks and performs the respective action against
     that exception.
METHODS OF EXCEPTION HANDLING
1- if - else method
    main()
    {
    code…
    code…
    EXCEPTION
    if (n==exception condition)
             printf(“Operation cannot be performed”);
             /*here we have to decide whether to exit the program or to
             ignore the exception and run the program anyway */
    else
             continue
    code…
    }
METHODS OF EXCEPTION HANDLING
2- setjmp () and longjmp() method:
    • setjmp() and longjmp() are used to jump away from a
    particular location in a program into another function.
    • The programmer written code, inside that function
    handles the exception
    • To test the occurrence of an exception the setjmp()
    function is called up.
    • setjmp() saves the most recent event of the program
    before that statement in a buffer called jmp_buf.
METHODS OF EXCEPTION HANDLING
2- setjmp () and longjmp() method:
    • As soon as the exception test is complete the setjmp()
    returns a value to the function.
    • If the value returned from the setjmp() to the longjmp()
    is ‘0’ it means that there was an exception condition.
    • Now the program will again start from the same point
    where it stopped (saved in the buffer), until the exception
    condition is removed or repaired.
    • On the other hand programmer can also display error
    messages or other fool-proof techniques.
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Programs Difficulty Level 1
     • Causes of Exception Occurrence
     • Programs Difficulty Level 2
     • Programs Difficulty Level 3
     • Grading
     • Q/A Session
PROGRAM DIFFICULTY LEVEL 1

PROGRAM 1:

      #include <stdio.h>
       void main (void)
      {
               int a , b , c ;
               a=1;
               b=0;
               c=a/b
               printf(“%d”,c);
       }
#include <stdio.h> /* for fprintf and stderr */
#include <stdlib.h> /* for exit */                                                  PROGRAM 2
int main( void )                                                                          DL 1
{
  float dividend = 50;
  float divisor ;
  float quotient;

    printf("Enter a divisor for a dividend of 50nn");
    scanf("%f", &divisor);
    if (divisor == 0)
     {
       /* Example handling of this error. Writing a message to stderr, and
        * exiting with failure.
        */
                 fprintf(stderr, "nnDivision by zero !!! Aborting... ={ nn");
       exit(EXIT_FAILURE); /* indicate failure.*/
     }


    quotient = (dividend/divisor);
    printf("n%.2fnn",quotient);
    exit(EXIT_SUCCESS); /* indicate success.*/
}
#include <setjmp.h>
#include <stdio.h>                                                           PROGRAM 3
#include <stdlib.h>
void main(void)
                                                                                   DL 1
{
  float dividend = 50;
  float divisor ;
  float quotient;
  jmp_buf env;


    printf("Enter a divisor for a dividend of 50nn");
    setjump(env);                                         Stores the scanf() statement in a buffer
    scanf("%f", &divisor);

    if (divisor == 0)                                     Exceptional Case
     longjmp(env,2);                                      Throws back to where we paused

    quotient = (dividend/divisor);
    printf("n%.2fnn",quotient);
    exit(EXIT_SUCCESS); /* indicate success.*/

    return 0;
}
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Programs Difficulty Level 1
     • Causes of Exception Occurrence
     • Programs Difficulty Level 2
     • Programs Difficulty Level 3
     • Grading
     • Q/A Session
CAUSES OF EXCEPTION HANDLING
Exception Class               Cause
ArgumentException             An argument to a method was invalid.
                              A null argument was passed to a method that
ArgumentNullException
                              doesn't accept it.
ArgumentOutOfRangeException   Argument value is out of range.
ArithmeticException           Arithmetic over - or underflow has occurred.
                              Attempt to store the wrong type of object in an
ArrayTypeMismatchException
                              array.
DivideByZeroException         An attempt was made to divide by zero.
FormatException               The format of an argument is wrong.
NotFiniteNumberException      A number is not valid.
NullReferenceException        Attempt to use an unassigned reference.
OutOfMemoryException          Not enough memory to continue execution.
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Programs Difficulty Level 1
     • Causes of Exception Occurrence
     • Programs Difficulty Level 2
     • Programs Difficulty Level 3
     • Grading
     • Q/A Session
#include <stdio.h>      /* fprintf */
#include <errno.h>       /* errno */                                     PROGRAM 1
#include <stdlib.h>
#include <string.h>
                        /* malloc, free, exit */
                        /* strerror */
                                                                               DL 2
extern int errno;
int main( void )
{
  /* pointer to char, requesting dynamic allocation of 2,000,000,000
   * storage elements (declared as an integer constant of type
   * unsigned long int). (If your system has less than 2GB of memory
   * available, then this call to malloc will fail)
   */
  char *ptr = malloc( 2000000000 );

    if ( ptr == NULL )
       puts("malloc failed");
    else
    {
       /* the rest of the code hereafter can assume that 2,000,000,000
        * chars were successfully allocated...
        */
       free( ptr );
    }

    exit(EXIT_SUCCESS); /* exiting program */
}
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Programs Difficulty Level 1
     • Causes of Exception Occurrence
     • Programs Difficulty Level 2
     • Programs Difficulty Level 3
     • Grading
     • Q/A Session
PROGRAM 1
      DL 3
PRESENTATION CONTENTS
     • Team/Topic Introduction
     • Errors & its Types
     • Exception Handling Basics
     • Occurrence & Handling of an Exception ( Flow diagrams )
     • Methods to handle an Exception
     • Programs Difficulty Level 1
     • Causes of Exception Occurrence
     • Programs Difficulty Level 2
     • Programs Difficulty Level 3
     • Grading
     • Q/A Session
GROUP C GRADING
Group leader: Raza Najam

Members Name    Searching Teamwork PPT Skills Willing to C Presentation Skills Total/10
                   10        10       10           10              10              5
Hassaan Idrees          8         8         7             7                   8       3.8
Syed Ahmed Fuad         8         8         5             9                   7       3.7
Tanveer Hussain         8         8         5             6                   5       3.2
Waleed Raza             7         7         5             5                   4       2.8
Hasan Qamar         -         -        -            -               -               -
Q/A

Weitere ähnliche Inhalte

Was ist angesagt?

Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
Database Triggers
Database TriggersDatabase Triggers
Database TriggersAliya Saldanha
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in JavaAbhilash Nair
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++Lahiru Dilshan
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming Rokonuzzaman Rony
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic InheritanceDamian T. Gordon
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentationNITISH KUMAR
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Deepam Aggarwal
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table1keydata
 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
 

Was ist angesagt? (20)

Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Constructor
ConstructorConstructor
Constructor
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 

Ähnlich wie Exception handling in c programming

Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptlecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptZeeshanAli593762
 
Exception handling
Exception handlingException handling
Exception handlingKarthik Sekar
 
Programming_Lecture_1.pptx
Programming_Lecture_1.pptxProgramming_Lecture_1.pptx
Programming_Lecture_1.pptxshoaibkhan716300
 
Algorithms and flow charts
Algorithms and flow chartsAlgorithms and flow charts
Algorithms and flow chartsChinnu Edwin
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handlingSmitha Padmanabhan
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem slovingMani Kandan
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxVishuSaini22
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++Mukund Trivedi
 
exceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptexceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptyjrtytyuu
 
Unit 1 program development cycle
Unit 1 program development cycleUnit 1 program development cycle
Unit 1 program development cycleDhana malar
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.pptJAYESHRODGE
 
exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2thenmozhip8
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptionsSujit Kumar
 

Ähnlich wie Exception handling in c programming (20)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptlecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Programming_Lecture_1.pptx
Programming_Lecture_1.pptxProgramming_Lecture_1.pptx
Programming_Lecture_1.pptx
 
Algorithms and flow charts
Algorithms and flow chartsAlgorithms and flow charts
Algorithms and flow charts
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
exceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptexceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.ppt
 
Unit 1 program development cycle
Unit 1 program development cycleUnit 1 program development cycle
Unit 1 program development cycle
 
Debbuging
DebbugingDebbuging
Debbuging
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
 
exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
 

KĂźrzlich hochgeladen

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂşjo
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

KĂźrzlich hochgeladen (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Exception handling in c programming

  • 1. ALGORITHMS AND COMPUTING ( LAB ) END SEMESTER PRESENTATIONS GROUP C
  • 2. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  • 3. GROUP MEMBERS: • Raza Najam • Hassaan Idrees • Waleed Raza • Syed Ahmed Fuad • Tanveer Hussain • Hassan Qamar Rana
  • 4. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types Hassaan • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) Raza • Methods to handle an Exception Fuad • Example Program 1 ( DL 1) Tanveer • Causes of Exception Occurrence Waleed • Example Program 2 ( DL 2) Raza • Example Program 3 ( DL 3) • Grading • Q/A Session
  • 5. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  • 6. ERRORS AND ITS TYPES • There are three basic types of errors: 1- Syntax Error 2- Semantic Errors 3- Logical Errors These errors are detected by the ‘C’ compiler • There are also a few kinds of errors in ‘C’ those are not detected by the compiler. 4- Run Time Errors 5- Compile Time Errors These error are not identified by the compiler and so they are termed as “EXCEPTIONS”
  • 7. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  • 8. WHAT IS REALLY AN EXCEPTION? • An exception is an indication of a problem that occurs during a program’s execution. • Exception is a runtime problem that occurs rarely handling an exception allows programs to continue executing as if no problem had been encountered. • Helps terminating the program in a controlled manner rather in an unpredictable fashion • Exception handling enables programmers to create applications that can resolve (or handle) exceptions.
  • 9. DIFFERENCES BETWEEN AN ERROR AND AN EXCEPTION 1- Errors occur at compilation time as well as run time while exceptions mostly occur at run time. 2- Compile time errors are detected by the compiler while exceptions need to be predicted by the programmer himself 3- Errors occur frequently while exceptions, as the name suggests occur seldom 4- Error detection and debugging is easier while exception prediction and its handling is a bit complex procedure 5- Errors can be removed by removing syntax and logical mistakes while exception handling needs pre-defined or user defined procedures.
  • 10. BENEFITS: • Helps improve a program's fault tolerance. • Enables the programmer to remove error-handling code from the ‘main line’ of the program’s execution • Programmers can decide to handle any exceptions they choose – all exceptions of a certain type or all exceptions of a group of related types
  • 11. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  • 12. EXCEPTION HANDLING IN C • C does not provide direct support for error/exception handling. • By convention, the programmer is expected to develop an algorithm for an error and exception case free program. • Intelligent visualization skills and prediction of end-user actions on a particular event prevents errors from occurring in the first place. • The programmer is expected to test return values from a function.
  • 13. Exception handling is designed to: Process synchronous errors, which occur when a statement executes. Common examples of these errors are: 1. out-of-range array subscripts 2. arithmetic overflow 3. division by zero 4. invalid function parameters 5. unsuccessful memory allocation, due to lack of memory Exception handling is not designed to: Process errors associated with asynchronous events fro example: 1. Disk I/O completions 2. Network message arrivals 3. Mouse-clicks and keystrokes which occur in parallel with, and independent of, the program’s flow control.
  • 14. HOW TO HANDLE AN EXCEPTION: - Ignore the exception (Non – Professional ) - Abort the program ( Even Worst ) - Set error indicators ( Beginner’s Approach ) - Issue an error-message and call exit(). ( Non – Feasible ) - Use setjmp() and longjmp() ( Leading to Professional)
  • 15. THE CLASSIC ‘C’ APPROACH TO EXCEPTION HANDLING. • Each function returns a value indicating success or failure. • Every function must check the return code of every function call it makes and take care of errors. • Exceptions make it easy to separate error handling from the rest of the code. • Exceptions make it easy to separate error handling from the rest of the code. • Intermediate functions can completely ignore errors occurring in functions they call, if they can't handle them anyway
  • 16. Main body { Any program… Program Pauses and control is Exception Occurs shifted to some other function Further program End of program Now the user defined function decides what to do with that particular exception }
  • 17. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  • 18. METHODS OF EXCEPTION HANDLING There are two methods to handle an exception in C. 1- if - else method 2- setjmp() and longjmp() method
  • 19. METHODS OF EXCEPTION HANDLING 1- if - else method • Exception is handled by making decisions via if – else. • Occurrence of an exception is checked by the return values of a function or by defined parameters. • Along with that condition is placed an if – else statement that checks and performs the respective action against that exception.
  • 20. METHODS OF EXCEPTION HANDLING 1- if - else method main() { code… code… EXCEPTION if (n==exception condition) printf(“Operation cannot be performed”); /*here we have to decide whether to exit the program or to ignore the exception and run the program anyway */ else continue code… }
  • 21. METHODS OF EXCEPTION HANDLING 2- setjmp () and longjmp() method: • setjmp() and longjmp() are used to jump away from a particular location in a program into another function. • The programmer written code, inside that function handles the exception • To test the occurrence of an exception the setjmp() function is called up. • setjmp() saves the most recent event of the program before that statement in a buffer called jmp_buf.
  • 22. METHODS OF EXCEPTION HANDLING 2- setjmp () and longjmp() method: • As soon as the exception test is complete the setjmp() returns a value to the function. • If the value returned from the setjmp() to the longjmp() is ‘0’ it means that there was an exception condition. • Now the program will again start from the same point where it stopped (saved in the buffer), until the exception condition is removed or repaired. • On the other hand programmer can also display error messages or other fool-proof techniques.
  • 23. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  • 24. PROGRAM DIFFICULTY LEVEL 1 PROGRAM 1: #include <stdio.h> void main (void) { int a , b , c ; a=1; b=0; c=a/b printf(“%d”,c); }
  • 25. #include <stdio.h> /* for fprintf and stderr */ #include <stdlib.h> /* for exit */ PROGRAM 2 int main( void ) DL 1 { float dividend = 50; float divisor ; float quotient; printf("Enter a divisor for a dividend of 50nn"); scanf("%f", &divisor); if (divisor == 0) { /* Example handling of this error. Writing a message to stderr, and * exiting with failure. */ fprintf(stderr, "nnDivision by zero !!! Aborting... ={ nn"); exit(EXIT_FAILURE); /* indicate failure.*/ } quotient = (dividend/divisor); printf("n%.2fnn",quotient); exit(EXIT_SUCCESS); /* indicate success.*/ }
  • 26. #include <setjmp.h> #include <stdio.h> PROGRAM 3 #include <stdlib.h> void main(void) DL 1 { float dividend = 50; float divisor ; float quotient; jmp_buf env; printf("Enter a divisor for a dividend of 50nn"); setjump(env); Stores the scanf() statement in a buffer scanf("%f", &divisor); if (divisor == 0) Exceptional Case longjmp(env,2); Throws back to where we paused quotient = (dividend/divisor); printf("n%.2fnn",quotient); exit(EXIT_SUCCESS); /* indicate success.*/ return 0; }
  • 27. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  • 28. CAUSES OF EXCEPTION HANDLING Exception Class Cause ArgumentException An argument to a method was invalid. A null argument was passed to a method that ArgumentNullException doesn't accept it. ArgumentOutOfRangeException Argument value is out of range. ArithmeticException Arithmetic over - or underflow has occurred. Attempt to store the wrong type of object in an ArrayTypeMismatchException array. DivideByZeroException An attempt was made to divide by zero. FormatException The format of an argument is wrong. NotFiniteNumberException A number is not valid. NullReferenceException Attempt to use an unassigned reference. OutOfMemoryException Not enough memory to continue execution.
  • 29. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  • 30. #include <stdio.h> /* fprintf */ #include <errno.h> /* errno */ PROGRAM 1 #include <stdlib.h> #include <string.h> /* malloc, free, exit */ /* strerror */ DL 2 extern int errno; int main( void ) { /* pointer to char, requesting dynamic allocation of 2,000,000,000 * storage elements (declared as an integer constant of type * unsigned long int). (If your system has less than 2GB of memory * available, then this call to malloc will fail) */ char *ptr = malloc( 2000000000 ); if ( ptr == NULL ) puts("malloc failed"); else { /* the rest of the code hereafter can assume that 2,000,000,000 * chars were successfully allocated... */ free( ptr ); } exit(EXIT_SUCCESS); /* exiting program */ }
  • 31. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  • 32. PROGRAM 1 DL 3
  • 33. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  • 34. GROUP C GRADING Group leader: Raza Najam Members Name Searching Teamwork PPT Skills Willing to C Presentation Skills Total/10 10 10 10 10 10 5 Hassaan Idrees 8 8 7 7 8 3.8 Syed Ahmed Fuad 8 8 5 9 7 3.7 Tanveer Hussain 8 8 5 6 5 3.2 Waleed Raza 7 7 5 5 4 2.8 Hasan Qamar - - - - - -
  • 35. Q/A