定义异常

From Apache OpenOffice Wiki
Jump to: navigation, search


exception 类型包含有关错误的信息。如果操作检测到导致正常的进程流暂停的错误,必须提出异常,并通过 exception 对象将有关错误的信息发送回调用程序。这将导致调用程序打断其正常的程序流,并根据从异常对象中接收到的信息采取相应的措施。如果需要异常及其实现的更多信息,请参阅 专业 UNO - UNO 语言绑定专业 UNO - UNO 概念 - 异常处理


开发过程中要用到很多异常。在许多情况下都应该有足够的异常,因为消息字符串将被发送回调用程序。定义异常时,请使用以下方法,以便其他开发者可以在他们的上下文中重复使用。


异常声明的开头是关键字 exception,然后给出新异常类型的标识符,并在花括号中提供异常的主体,结尾是分号。异常主体包含异常成员声明的列表,这些声明由已知类型和异常成员的标识符定义。同样,成员的声明也必须以分号结尾。


异常必须基于 com.sun.star.uno.Exceptioncom.sun.star.uno.RuntimeException,并且直接或间接从这两个异常派生而来。com.sun.star.uno.Exception 只可以在产生它们的指定操作中抛出,而 com.sun.star.uno.RuntimeException 可以随时抛出。继承用冒号 : 表示,后面是父类型的全名

  // com.sun.star.uno.Exception is the base exception for all exceptions
  exception Exception {
      string Message;
      XInterface Context;
  };
 
  // com.sun.star.lang.IllegalArgumentException tells the caller which 
  // argument caused trouble
  exception IllegalArgumentException: com::sun::star::uno::Exception
  { 
      /** identifies the position of the illegal argument. 
          <p>This field is -1 if the position is not known.</p>
       */
      short ArgumentPosition; 
 
  }; 
 
  // com.sun.star.uno.RuntimeException is the base exception for serious errors
  // usually caused by programming errors or problems with the runtime environment
  exception RuntimeException : com::sun::star::uno::Exception {
  };
 
  // com.sun.star.uno.SecurityException is a more specific RuntimeException 
 
  exception SecurityException : com::sun::star::uno::RuntimeException {
  };
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages