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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:35:39+00:00 2026-05-25T16:35:39+00:00

Trying to figure out the best place to put a try catch statement when

  • 0

Trying to figure out the best place to put a try catch statement when a recursive call is placed. The factorial computation is done with long datatype. Expecting an exception to be thrown when the factorial becomes too huge to fit into a long variable.

However the code is showing factorial = 0 whenever it’s too large. No exception is being thrown. So is there a problem with the try-catch placement or does putting over-large numbers throw no exception?

class Fact
{
    static long fact(long n)
    {
       if(n==1)
           return 1;
        return n*fact(n-1);
    }

public static void main(String args[])
{
    try{
        long f = fact(555);
        System.out.println("Factorial = "+f);
    }
    catch(Exception e){
            System.out.println("Exception = "+e);
    }
}
}
  • 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-25T16:35:40+00:00Added an answer on May 25, 2026 at 4:35 pm

    Integer overflow doesn’t throw any exceptions in Java. Integer divide by zero throws ArithmeticException, but not overflow.

    The question has now morphed into “Why does this return zero?” And the answer is that it’s just a coincidence. If you modify the function like this:

    static long fact(long n)
    {
       if(n==1)
           return 1;
        long result =  n*fact(n-1);
        System.out.println(n + ", " + result);
        return result;
    }
    

    and then look at the output, you get (I deleted some lines in the middle and at the end):

    2, 2
    3, 6
    4, 24
    5, 120
    6, 720
    7, 5040
    8, 40320
    ...
    19, 121645100408832000
    20, 2432902008176640000
    21, -4249290049419214848
    ...
    60, -8718968878589280256
    61, 3098476543630901248
    62, 7638104968020361216
    63, 1585267068834414592
    64, -9223372036854775808
    65, -9223372036854775808
    66, 0
    67, 0
    ...
    

    and then once it’s hit zero, it’s zero ever after. After bouncing around and overflowing a few times, your product just accidentally hits a number with 0s in the least significant 64 bits. Strange, but true.

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

Sidebar

Related Questions

I'm trying to figure out the best place to put xdebug's excellent xdebug_time_index() function
I'm trying to figure out the best place to put multi-attribute search form logic
Trying to figure out the best way to set up collection lists for users
Trying to figure out the best way to do this: Should I do something
I'm trying to figure out the best way to determine whether I'm in the
I am trying to figure out the best way to model a spreadsheet (from
I'm trying to figure out the best way to insert a record into a
I'm trying to figure out the best lower-budget home office development hardware setup. My
I am trying to figure out the best way in an asmx web service
I'm trying to figure out the best way to save a simple one-to-many relationship

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.