How do exceptions really work?

Posted on 2006-06-07
Last Modified: 2010-04-16
When an exception occurs (wether because of an error or if it's explicitely thrown), what does really happen? If there's a catch block, it goes to that, if there's no catch block, it exits a function and goes to the next one on call stack? If the rest of the function where an exception happens doesn't depend on the code that caused the exception, can I just use goto in the catch block and ignore it?
Question by:gromul
    LVL 5

    Assisted Solution

    If an exception occurs and no try catch block is used, the program will terminate.  It will not execute the next function in the call stack.  You don't want to ignore exceptions, but if you want, you can just have a catch block that does nothing in the catch.

    For example:

    try {
      // simulate an exception getting thrown by something
      throw new ApplicationException();
    catch {

    // this function will still be executed


    LVL 4

    Accepted Solution

    A try...catch block is used for Error handling.
      That does not mean that you cant write a program witout try..catch block, you could. You check everything and see if all status is okay and then you do your work. Infact this is how error handling is done before try...catch was introduced. The concept of try..catch is you assume that everything is okay, and you just work around, you dont worry about the error. You just include the code that has the chances to fail inside the try block. and you write the code inside the catch block which has to executed when the try block fails. That is it. The program is executed, and any error is occured, the try block is stopped at that point and the catch block is executed, and the rest of the code is exectued normally, unless you quit the function inside the catch block. If no error is occured, then the catch block is omited.
    So simple as that.
    There is another part in try...catch, it is called finally. not everyone uses this, it is because it as same as the code that is placed after the try...catch block.

       code block 1

      code block 2
     code block 3
    code block 4

       The code blocks 3 and 4 is executed always. code block 2 is only executed if any error is occured in code block 1. Code block 1 is executed and if any error occurs, it bypasses the rest code in code block 1

      So you can the finally block when u have some clean up process which is used in the try...catch block.
    You can also have many cacth block each catching different execptions
    for further reference goto

    LVL 7

    Assisted Solution


    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now