Reputation: 549
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: ");
}
}
}
Upvotes: 2
Views: 3098
Reputation: 332571
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:
Upvotes: 2
Reputation: 308763
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.
Upvotes: 5
Reputation: 3274
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
}
Upvotes: 4
Reputation: 22571
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
}
Upvotes: 2
Reputation: 2519
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
Upvotes: 2