簡體   English   中英

投擲 Class<!--? extends Throwable-->

[英]throwing Class<? extends Throwable>

所以我有一個 function 看起來像這樣:

    public void addExceptionCommands(Class<? extends Throwable> exClass, Command... commands) {
        for (Command command : commands) {

            try {
                //Push the command to the stack of executed commands
                executedCommands.push(command);
                command.execute();

            } catch (CouldNotExecuteCommandException e) {
                // Default strategy is to rollback
                rollback();
                // Log
                e.printStackTrace();
                //I want to throw exClass here
            }
        }
    }

我想拋出exClass,如何實現? 拋出 exClass 不起作用

編輯:謝謝大家的所有答案,我最終使用了供應商:D

當您有 class 類型時,您可以執行類似的操作

throw exClass.newInstance(); 

您只能拋出Throwable的子類,並且Class不是。

但是,您可以修改您的方法以接受生成新 Throwable 的供應商,然后您可以將其拋出:


    public <T extends Throwable> void addExceptionCommands(Supplier<T> exceptionSupplier, Command... commands) throws T {
        for (Command command : commands) {
            try {
                //Push the command to the stack of executed commands
                executedCommands.push(command);
                command.execute();
            } catch (CouldNotExecuteCommandException e) {
                // Default strategy is to rollback
                rollback();
                // Log
                e.printStackTrace();
                //I want to throw exClass here

                final T exception = exceptionSupplier.get();
                exception.addSuppressed(e);

                throw exception;
            }
        }
    }

然后,您可以像這樣調用您的方法:

addExceptionCommands(YourException::new, command1, command2, ...);

該參數是一種異常類型 如果你拋出一些東西,它必須是一個異常的實例

我認為這不會像你想的那樣奏效。

如果您希望調用者定義拋出的異常類型,那么讓調用者在自己的代碼中這樣做 調用者可以捕獲您的方法拋出的異常,並將其包裝在它選擇的任何異常中。

public void addExceptionCommands( Command... commands) 
throws CouldNotExecuteCommandException {
  ...
}

...
   try {
       commandable.addExceptionCommands( myCommands );
   } catch (CouldNotExecuteCommandException e) {
      // Wrap the command exception in my own.
      throw new MySpecialException( "My message", e );
   }

如果要從命令中支持各種異常,請考慮 Java 的java.util.concurrent package 提供的示例。 考慮ExecutorService.submit()方法和Future.get()方法。 提交給 executor 的任務可以拋出各種各樣的異常。 但是Future.get()將拋出的任何異常包裝在一個定義明確並聲明的ExecutableException中。

嘗試使用java.lang.reflect.Constructor

Constructor.newInstance()優於Class.newInstance()因為它允許您使用 arguments 來創建新實例。

Constructor constructor = exClass.getDeclaredConstructor();
Throwable ex = (Throwable) constructor.newInstance(); 
throw ex;

使用String參數(用於消息?)

Constructor constructor = exClass.getDeclaredConstructor(String.class);
Throwable ex = (Throwable) constructor.newInstance("message goes here"); 
throw ex;

https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html

此外, Class.newInstance()已被棄用。 https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#newInstance--

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM