尝试使用Java中的Catch Block

时间:2009-07-23 23:52:43

标签: java exception try-catch

所以我给了这个代码,我必须创建一个Exception,然后使用Try/Catch Block来捕获它。我已经在代码的底部制作了Exception。但我以前从未使用过Try/Catch Block,也不确定如何实现它。

例外情况是输入未在enum下列出的排名。我也需要使用带有捕获异常的toString,但我很确定我可以解决这个问题。

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: ");

      }   
  }
}

5 个答案:

答案 0 :(得分:5)

您不需要该例外。当您将Title枚举添加为传递给Staff构造函数的类型时,就无法在枚举中提供的值。你永远不会得到无效的头衔。这就是枚举的全部内容。

更新:这里有一个小代码审核订单。

  1. 您的默认构造函数相当奇怪。你可以成为没有名字或工资的部门负责人吗?此处调用“this”是合适的,并且更好的默认值是有序的。
  2. 仅限工资的整数 - 好的。没单位?美元?欧元?
  3. 薪水可以是负数吗?那有意义吗? (自我注意:不要在那里工作。)
  4. 为什么你需要toString和display?什么是显示覆盖?我建议放弃显示并坚持使用toString。
  5. 您的输入法没有任何意义。
  6. 为什么Exception是一个内部类?

答案 1 :(得分:4)

try / catch用于捕获try子句中方法抛出的异常。如果try中的方法没有抛出任何异常,那么try / catch就没有意义了。 现在你做了你的例外,但是没有方法可以抛出你的异常。

这是关于如何使用例外的简单示例:



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: ");

      }   
  }

现在,无论何时运行MyTest.myMethod(),编译器都需要围绕该调用的try / catch。


    MyTest test = new MyTest();
    try
    {
       test.myMethod();
    }
    catch(InvalidRankException ex)
    {
       //Something went wrong
    }

答案 2 :(得分:2)

不完全确定你要做什么,但try-catch块的工作原理如下:

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

您还必须修改您正在尝试的方法,以便它抛出您正在寻找的Exception:

public void doSomething(String blah) throws Exception

答案 3 :(得分:2)

捕获异常非常简单:

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

抛出异常非常简单:

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

如果您的异常不是从RuntimeException下降,那么您必须声明该方法抛出它:

public void myExceptionCausingMethod() throws MyException{
  //Method code
}

答案 4 :(得分:2)

try / catch语句包含一些代码,用于处理该代码中可能出现的错误和异常。

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
    }
  }
}

这里有两个问题:

  1. Enum不会返回无效的排名/标题
  2. InvalidRankException不会测试导致它触发的任何内容。