Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:56:10+00:00 2026-05-25T18:56:10+00:00

In Java, if a specific line of code causes the program to crash, then

  • 0

In Java, if a specific line of code causes the program to crash, then the exception is caught and the program continues to execute.

However, in C++, if I have a piece of code that causes the program to crash, like:

try
{
    int x = 6;
    int *p = NULL;
    p = reinterpret_cast<int*>(x);

    *p = 10; // the program crashed here

    cout << "x = " << *p << endl;
}
catch(const char* Message)
{
    cout << "There is an run-time error";
}

Then the program still crash and the exception is not caught.

So what is the point of exception handling in C++? Am I misunderstanding something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:56:10+00:00Added an answer on May 25, 2026 at 6:56 pm

    The line that crashes is dereferencing an invalid pointer. In C++ this will not throw an exception. Instead it is undefined behaviour.

    There’s no such thing as a null pointer exception in C++, unlike Java which will throw a null pointer exception. Instead dereferencing an invalid pointer will lead to undefined behaviour. Undefined behaviour does not always imply a crash, however if it crashes you’re lucky.

    Language overview:

    Finally and RAII

    One of the most significant differences between C++ and Java is that Java supports a finally statement. Code in the finally block is always run regardless of whether code in the preceding catch block is executed or not. For example:

    try
    {
    }
    catch (SomeException e)
    {
    }
    finally
    {
      //code here is always exectued.
    }
    

    The purpose of the finally statement is to allow the programmer cleanup at that point, i.e. release sockets, close file handles etc… Even though Java runs a garbage collector, garbage collection only applies to memory and no other resource. There are still occasions where you have to manually dispose of resources. Now C++ doesn’t have a finally statement so users of the language are advised to adhere to the RAII principle (Resouce Acquisition is Initialization) Stroustrup has an explanation about it here: http://www.stroustrup.com/bs_faq2.html#finally. I prefer to call it Resource destruction is deallocation but basically when your object falls out of scope, invoking the destructor, then that destructor should release whatever resources the object maintained.

    For example, C++11x provides a std::unique_ptr to manage this:

    void foo()
    {
      std::unique_ptr<T> t(new T)
      try
      {
        //code that uses t
      }
      catch (...)
      {
      }
    }
    

    The resource allocated via new will be deleted when the function ends.

    catch all statements

    Because all exceptions in Java inherit from a common base class Exception if you want your catch clause to catch any exception, then set it up like this:

    catch (Exception e)
    {
      //any exception thrown will land here.
    }
    

    In C++ there’s no restriction on what can be thrown and no common base class for all exceptions. Standard practice is to form your custom exception class by inheriting from std::exception but the language doesn’t enforce this. Instead there’s a special syntax for catching all exceptions:

    catch (...)
    {
    
    }
    

    Unhandled exceptions

    This is another area where the languages behave differently. In C++ a thrown exception that is not caught will call std::terminate. std::terminate’s default behaviour is to call abort which generates a SIGABRT and the entire program stops.

    In Java the behaviour is to print a stack trace and terminate the thread that the uncaught exception occured in. However because a Java programmer may provide an UncaughtException handler, the behaviour could quite well be different from the default of terminating the thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a specific project where I need to wrap every code line at
I have a simple java code which gets html text from the input url:
I need to execute an external batch file in java with a specific timeout.
Simply, are there any Java Developer specific Linux distros?
I'm not quite sure if this is specific to Sun Java Systems Application Server
What is the specific reason that clone() is defined as protected in java.lang.Object ?
Exists a way to call .net assemblies more specific .dll files in java? I
I realize that since UNIX sockets are platform-specific, there has to be some non-Java
I'm building a very simple Java parser, to look for some specific usage models.
Java Newbie here. I have a JFrame that I added to my netbeans project,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.