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 6622253
In Process

The Archive Base Latest Questions

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

I can do this, no problem: long lngval = 3L; int i = lngval;

  • 0

I can do this, no problem:

long lngval = 3L;
int i = lngval;

but if I try this:

try {
  throw 3L;
}

catch(int i) {
  cout << "caught " << i << endl;
}

I get an unhandled exception.

This seems inconsistent. what is the reason for no type conversion in this case?

  • 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-25T21:22:56+00:00Added an answer on May 25, 2026 at 9:22 pm

    In the first case, the compiler can tell exactly what you want to do: convert a long to an int. In the second case, the compiler has to assume that you might have a construct like this:

    try {
      try {
        throw 3L;
      }
      catch (int i) { /* P */ }
    }
    catch (long l) { /* Q */ }
    

    The idea is that the compiler can never know whether there might be a catch (long l) lurking outside the current context, so it’s never safe to just pick the first possible conversion.

    This is also why it’s common to use a class hierarchy when throwing exceptions rather than random types like int or long: it makes it easy to add more or less specification to your exception handlers in such a way that the compiler can be sure of your intentions (via the is-a relationship).

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

Sidebar

Related Questions

I'll try to explain this problem the best way i can with code: double
I'm facing this problem again and again, but can't find any solution (except mine,
This problem looks trivial, but I can't find the solution. When I create a
I am thinking about this problem now for very long. I try to use
I have been thinking about this problem for long enough but cannot come up
(Note: I have solved this problem, but it took long enough that I'm posting
I think that this problem can be sorted using reflection (a technology which I'm
I need some ideas on how I can best solve this problem. I have
Sorry for the vague title, as I really can't explain this problem succinctly. Basically
I had this problem before and can't for life of me remember how to

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.