How can I correct this to get it to compile. I need to use an exception handler and not correct the code

28 views Asked by At
class Exception3
{
    public static void main(String[] args)
    {
        if (Integer.parseInt(args[0]) == 0)
            throw new Exception("Invalid Command Line Argument");
    }
}
    
    

*The error involves an OutOfBoundsException

1

There are 1 answers

0
Zac On

Your question is very vague, but are you looking to do something like this? I assume this is a java related question.

This uses a try catch to attemt to parse the int. If an exception is thrown by the parseInt function than the catch block will catch the exception.

 1  class Exception3
 2  {
 3     public static void main(String[] args)
 4     {
 5        try{
 6           if (Integer.parseInt(args[0]) == 0)
 7           {
 8              // its okay?
 9           }
10        }
11        catch(OutOfBoundsException e)
12        {
13            // handle exception
14        }
15     }