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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:12:24+00:00 2026-05-21T02:12:24+00:00

I wrote a code which checks all kinds of conditions. If it meets the

  • 0

I wrote a code which checks all kinds of conditions.

If it meets the condition it does what it is supposed to, otherwise I want it to throw an
exception.

Is there any special syntax for that? Otherwise the compiler wants me to return any array,
which I don’t want to, due to the pre-condition.

Here is part of my code:

public static int [] code(int[]arr){
    if ((arr!=null)&&(chack4and5(arr))&&(arr[arr.length-1]!=4)&&(TwoFours(arr))){
        int k=0;
        for(int i = 0; i<=arr.length-1; i++){
            if (arr[i] == 4){
                int place= pos(arr,k);
                arr[place]=arr[i+1];
                arr[i+1]=5;
                k=k+3;  
            }
        }
        return arr;
    }
    else { 
        System.out.println("Please enter a legal array which matches the pre- conditions");
        }
}

}

  • 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-21T02:12:25+00:00Added an answer on May 21, 2026 at 2:12 am

    The way to throw an exception is

    throw new IllegalArgumentException(
            "Please enter a legal array which matches the pre- conditions");
    

    IllegalArgumentException is a Java runtime exception suitable for the current situation, but of course you can choose another one, or create and use your own type too. The only restriction is that it must be a subclass of java.lang.Exception.

    I would rearrrange your code though to check the preconditions first, then proceed if everything’s fine – I find this more readable:

    if (arr == null || !chack4and5(arr) || arr[arr.length-1] == 4 || !TwoFours(arr)) {
      throw new IllegalArgumentException(
            "Please enter a legal array which matches the pre- conditions");
    }
    int k=0;
    
    for(int i = 0; i<=arr.length-1; i++){
        if (arr[i] == 4){
            int place= pos(arr,k);
            arr[place]=arr[i+1];
            arr[i+1]=5;
            k=k+3;
        }
    }
    return arr;
    

    (In fact, I would even prefer extracting the precondition check into a separate method – but I leave this to you.)

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

Sidebar

Related Questions

I wrote some code which monitors the output of Stopwatch using a tight loop.
So, I wrote a code which uses some Microsoft Sql server dlls, these dlls
I have wrote the code below which was taken from Java How to program
I'm working with the jQuery Validation plugin and I wrote the following code which
I wrote code to get name of printer which is installed in my system.but
I wrote few lines of code which doesn't work correctly. Why? Could sb explain
Below is the code which I wrote to display a tooltip content. As the
I have been refactoring throwaway code which I wrote some years ago in a
Some time ago, I wrote some code to decide which method of updating mutable
I wrote a code sometime ago in which the object of ResultSet class res

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.