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

  • Home
  • SEARCH
  • 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 7829453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:46:13+00:00 2026-06-02T10:46:13+00:00

I am currently taking a introductory course in Java and this is regarding try-catch

  • 0

I am currently taking a introductory course in Java and this is regarding try-catch method. When I type this my System.out.println statement keeps repeating endlessly. Here is my code:

public static double exp(double b, int c) {
    if (c == 0) {
        return 1;
    }

    // c > 0
    if (c % 2 == 0) {
        return exp(b*b, c / 2);
    }
    if (c<0){
        try{
        throw new ArithmeticException();
        }
        catch (ArithmeticException e) {
            System.out.println("yadonegoofed");
        }
    }

    // c is odd and > 0
    return b * exp(b, c-1);
}
  • 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-06-02T10:46:15+00:00Added an answer on June 2, 2026 at 10:46 am
    if (c<0){
        try{
        throw new ArithmeticException();
        }
        catch (ArithmeticException e) {
            System.out.println("yadonegoofed");
        }
    }
    
    // c is odd and > 0
    return b * exp(b, c-1);
    

    Your comment c is odd and > 0 is incorrect — you never actually terminated the function with the exception. You threw it, you immediately caught it, and then continued to execute the recursive function. Eventually, when you hit wraparound, it’ll be a positive number again, and the errors won’t happen. (It’s about two billion iterations away — don’t wait.)

    I would not use an exception here — you just need to terminate the recursion. I’d check for negative input before checking for 0, and throw the exception there, and catch the exception in the caller.

    In pseudo-code:

    exp(double b, int c) {
        if (c < 0)
            throw new Exception("C cannot be negative");
        } else if (c % 2 == 0) {
            return exp(b*b, c / 2);
        } else {
            /* and so forth */
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently taking a course in Java and I've run into some confusing code.
I am currently taking a university course in data structures, and this topic has
Hi i am currently taking a Matrix, P, and doing this P500 = MatrixPower[P,
It is currently said that MD5 is partially unsafe. Taking this into consideration, I'd
I'm currently taking on a new project at home. In this project I'm going
We have a CMS system that we're currently taking the admin back end and
currently I am taking screenshots of my test failures this way: @AfterMethod(alwaysRun=true) public void
I'm currently taking a computer security class and would like to try to port
I am currently building this Android application, where I will be taking a screenshot
I am currently taking a c++ course and trying to get a deep understanding

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.