-5

Java newbie here and I'm doing an exercise trying to catch thrown exceptions by using as few try...catch statements as possible. When I compiled the code, I received IllegalArgumentException errors and wasn't sure how to fix this by using the try...catch statements.

I've watched tutorials, looked at other examples but since this is a new concept for me, I'm still unsure how to use the try...catch for this particular exercise.

public class Main extends Object {

public static void main(String [] args) {
    tryGetMax();
    tryRemove();

private static final void tryGetMax() {
    int max = 0;
    max = FunMethods.getMax((Integer[])null);
    Integer[] numbers = new Integer[50];
    Random rand = new Random();
    for (int i = 0; i < 50; i++) {
        numbers[i] = new Integer(rand.nextInt(500));
    }
    numbers[32] = null;
    max = FunMethods.getMax(numbers);
    numbers[32] = new Integer(rand.nextInt(500));
    max = FunMethods.getMax(numbers);
}

The second part of the exercise:

private static final void tryRemove() {
    FunMethods.remove(null, 2);
    Object[] someObjects = new Object[12];
    someObjects[0] = "a string!";
    someObjects[1] = new Integer(32);
    someObjects[2] = new Float(42.5f);
    someObjects[3] = "another string";
    for (int i = 4; i < someObjects.length; i++) {
        someObjects[i] = String.valueOf(i);
    }
    FunMethods.remove(someObjects, 12);
    someObjects = FunMethods.remove(someObjects, 3);
Tim
  • 41,901
  • 18
  • 127
  • 145
MJW
  • 41
  • 1
  • 1
  • 4
  • 4
    I don't see anywhere in your code that you've even attempted to add a try/catch statement. Did you read the documentation or look at any examples? What specifically didn't you understand? – tnw Jul 20 '15 at 19:58
  • You're right, I haven't yet. This is my confusion in terms of what lines to add the try/catch statements to make this compile correctly. – MJW Jul 20 '15 at 20:04
  • SO isn't for "teach me how to do basic stuff", sorry – Tim Jul 20 '15 at 20:05
  • Everyone started somewhere, including you. I'm just trying to understand how to incorporate it into this exercise. – MJW Jul 20 '15 at 20:07
  • You're absolutely right, but SO isn't the place to ask these kind of questions – Tim Jul 20 '15 at 20:16
  • I'm not sure how to respond to that. I'm only a month into learning Java and figured this would be a good place to ask, with a lot of knowledgeable people. – MJW Jul 20 '15 at 20:23
  • "When I compiled the code, I received `IllegalArgumentException`" You received an `IllegalArgumentException` at **compile** time or **runtime**? Your statement reads as compile time, but an `IllegalArgumentException` is typically encountered at runtime. – MadConan Jul 20 '15 at 20:41
  • Hi @MJW. Welcome to SO. I offer a few words of advice. I have observed a significant correlation among down-voted questions and proclamations along the lines of "I watched the tutorials." I believe these videos are doing a disservice to the aspiring student in that they offer a false sense of understanding to the viewer. Their primary objective is garnering advertisement impressions rather than teaching. I heartily suggest the venerable Java Trail tutorials: docs.oracle.com/javase/tutorial/java. There is no short-cut to mastery. Follow the trail and you will be rewarded. – David J. Liszewski Jul 21 '15 at 05:26

2 Answers2

0
try{
  //Code that can potentially throw an exception
} catch (IllegalArgumentException e){
  //Code to run if exception is throw
}

The exception is assigned to the variable e which has certain methods that could be called.

cbender
  • 2,231
  • 1
  • 14
  • 18
  • Thank you, I understand what try and catch do but I'm unsure what lines of this exercise would require the try/catch statement. – MJW Jul 20 '15 at 20:05
  • 1
    @MJW You need to find which methods that you consume or use can throw that exception. Go through the javadocs of the API you used. I am guessing its the Random class methods – Vivin Jul 20 '15 at 20:12
  • I forgot that part, it's the exceptions thrown by the FunMethods class. – MJW Jul 20 '15 at 20:16
0

ok, right under the public static void main(String[] args) { you should have try {. Then, at the end of the main method, you should have } catch (IllegalArgumentException e) {. And under that, you should have your code for catching that exception, and }.

Now this:

public static void main(String [] args) {
    tryGetMax();
    tryRemove();

Looks like this:

public static void main(String [] args) {
    try {
        tryGetMax();
        tryRemove();
    } catch(IllegalArgumentException e) {
        //this code runs if e is thrown
    }
The Eye
  • 59
  • 12