views:

1061

answers:

5

So I'm given this code and I have to create an Exception and then use a Try Catch Block to catch it. I've already made the Exception, at the bottom of the code. But I've never used a Try Catch Block before and am not sure how to implement it.

The Exception is if a rank that isn't listed under the enum is entered. I need to use a toString with the caught exception as well, but I'm pretty sure I can figure that one out.

package pracapp4;

import java.util.Scanner;

public class Staff extends Employee
{

  enum Title
  {
    DEPARTMENT_HEAD, DIRECTOR, DEAN, VICE_CHANCELLOR, CHANCELLOR
  }

  private Title title;

  public Staff()
  {
    super();
    title = Title.DEPARTMENT_HEAD;
  }

  public Staff(String firstName, String lastName, int salary, Title title)
  {
    super(firstName, lastName, salary);
    this.title = title;

  }

  @Override
  public String toString()
  {
    return super.toString() + "\n\tTitle: " + title;
  }


  @Override
  public void display()
  {

    System.out.println("<<Staff>>" + this);

  }

  @Override
  public void input(Scanner in)
  {
    super.input(in);

    if (in.hasNext())
    {
      this.title = Enum.valueOf(Title.class, in.next());
    }
  }

  class InvalidRankException extends Exception
  {
      public InvalidRankException()
      {
       super ("Unknown Rank Name: ");

      }   
  }
}
+1  A: 

Not exactly sure what you're trying to do, but try-catch blocks work like this:

try{ 
    throw new Exception("Example exception");
}
catch(Exception e){
    System.out.println( "Exception caught: " + e.getMessage() );
}

You'll also have to modify the method that you are trying so that it throws the Exception you're looking for:

public void doSomething(String blah) throws Exception
Blue
+1  A: 

Catching an exception is as simple as:

try{
  //Some code that throws MyExceptionClass
}catch(MyException e){
  //Some code that handles the exception e
}

Throwing an exception is as simple as:

throw new MyException(some, parameters, of, your choice);

If your exception doesn't descend from RuntimeException then you must declare the the method throws it:

public void myExceptionCausingMethod() throws MyException{
  //Method code
}
Kevin Montrose
+4  A: 

You don't need that exception. The moment you add your Title enum as the type you pass into the Staff constructor it's impossible to provide a value that's not in the enum. You'll never get an invalid title. That's the whole point of enum.

UPDATE: A little code review is an order here.

  1. Your default constructor is rather odd. You can be department head without a name or salary? A call to "this" is appropriate here, and better default values are in order.
  2. Whole numbers only for salary - OK. No units? USD? Euro?
  3. Can salary be negative? Does that make sense? (Note to self: Don't work there.)
  4. Why do you need both toString and display? What is display overriding? I'd recommend ditching display and sticking with toString.
  5. Your input method makes no sense whatsoever.
  6. Why is that Exception an inner class?
duffymo
+3  A: 

try/catch are used to catch exceptions thrown by methods inside the try clause. If the methods inside the try does not throw any exceptions then the try/catch will not makes sense. Right now you made your exception but there is no method that throws your exception.

This is simple example on how to use exceptions:



public class myTest
{

  public void myMethod() throws InvalidRankException
  {
     //Logic here
    if(something_is_wrong)
    {
        throw new InvalidRankException("Invalid Rank on myMethod due ...");
    } 

}

  class InvalidRankException extends Exception
  {
      public InvalidRankException()
      {
       super ("Unknown Rank Name: ");

      }   
  }

Now, whenever you run MyTest.myMethod() the compiler will require a try/catch surrounding that call.


    MyTest test = new MyTest();
    try
    {
       test.myMethod();
    }
    catch(InvalidRankException ex)
    {
       //Something went wrong
    }
Freddy
+1  A: 

The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code.

public void input(Scanner in) throws InvalidRankException {
  super.input(in);

  if (in.hasNext()) {
    try {     
      title = Enum.valueOf(Title.class, in.next());
    } catch(InvalidRankException ire) {
      //You've hit the exception, code in here how to handle the situation
    }
  }
}

There's two issues here:

  1. Enum won't return an invalid rank/title
  2. InvalidRankException doesn't test for anything to cause it to fire.
OMG Ponies