方法(远程方法调用)是否应返回一个布尔true值,指示即使抛出所有可能的异常,操作也会成功执行?
例:
在我的java应用程序中有许多CRUD远程方法调用,我捕获所有可能的异常并向调用客户端抛出自定义异常.
我现在应该返回void还是布尔值,因为Exceptions已经隐含地指示了操作的成功或失败?
如何在方法上抛出和UnsupportedOperationException?所以,如果我有一个Iterable对象,并且我试图禁止该对象的remove方法.
在下面的方法中,我返回一个可迭代的对象,其迭代器的删除我需要通过抛出UnsupportedErrorException来禁用.我可以在方法体内做到这一点或如何做到这一点?
public Iterable<String> getInNodes (String destinationNodeName) {
if (!hasNode(destinationNodeName))
return emptySetOfString;
else {
for(String e : nodeMap.get(destinationNodeName).inNodes)
{
emptySetOfString.add(e);
}
return emptySetOfString;
}
}
Run Code Online (Sandbox Code Playgroud) 我用QUnit库做了一些Javascript单元测试,然后我想测试方法是否抛出RangeError.它以这种方式完成:
QUnit.test("Resolve slide animation from left", function( assert ) {
var myOwnCreator = new MyOwnCreator();
assert.equal(myOwnCreator.resolveAnimationType("slideSide", "show", "left"),
"slide-left-in");
assert.equal(myOwnCreator.resolveAnimationType("slideSide", "hide", "left"),
"slide-left-out");
assert.throws(myOwnCreator.resolveAnimationType("slideSide", "hide"),
new RangeError(),
" If animation type is 'slideSide', you have to provide correct direction"
+ "to identify animation properly, direction cannot be 'undefined'");
});
Run Code Online (Sandbox Code Playgroud)
第一次和第二次测试通过,因为通过函数(,, slide -...")解析的动画类是可以的.但第三次测试死了:
Died on test #3 at file...
Run Code Online (Sandbox Code Playgroud)
因为它抛出RangeError.它可以抛出RangeError,但我不明白如何在单元测试中捕获它,获取,确定"信息.如果我理解QUnit文档:QUnit抛出文档
我做的一切都好:我传递抛出错误的函数,预期错误的实例和预期的消息.我有人会决定帮助我,我会很高兴 - 提前谢谢你.
我有一个带@Aspect注释的类正在调用ProceedingJoinPoint#proceed()。此方法throws Throwable以及类的外观如下所示:
@Aspect
@Component
public class MyClass{
@Around("@annotation(myAnnotation)")
public Object myMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
//some code here
try {
return joinPoint.proceed();
} finally {
//some more code here
}
}
}
Run Code Online (Sandbox Code Playgroud)
它是确定了myMehtod投掷Throwable在这种情况下,我必须调用另一个方法是throws Throwable?我应该避免抛出Throwable并以某种方式将其转换为Exceptionor Error吗?
无论哪种情况,我都想知道为什么。谢谢。
我只是在学习Go编程语言.我刚刚读到了Go中错误处理的方式.有一个很好的解释,但我有一些混乱.在Go中处理错误的典型方法是将错误作为函数的第二个值返回:
f, err := Sqrt(-1)
if err != nil {
fmt.Println(err)
}
Run Code Online (Sandbox Code Playgroud)
非常简单.但是,假设我是一名图书馆开发人员,我可能想知道客户端我的函数可能会抛出一些错误,而客户端必须处理它.
在Java中,我有throws条款.因此,客户必须将它放在这些try...catch块中
在Go中,我可以从函数返回错误; 但是,如果图书馆的客户避免处理它,那么我该如何优雅地告诉他处理它?
编辑: 我只是在探索一种语言.不要试图将Java的思维与Go混合,尊重每个人的理念.我刚问过,因为我想知道是否有任何关键字,因为我还没有学到Go.
我有一个方法,我遍历List并创建List.在这样做时,我调用一个方法(createResult)来给一个Result也抛出CustomException,我将它包装为ResultClassException.但我一直收到一个错误,指出未处理的异常.
我的代码:
private List<Result> getResultList(List<String> results) throws ResultClassException {
List<Result> resultList = new ArrayList<>();
results.forEach(
(resultName) -> {
if (!resultRepository.contains(resultName)) {
try {
final Result result = createResult(resultName);
resultList.add(result);
} catch (CustomException e) {
throw new ResultClassException("Error",e);
}
} else {
resultList.add(resultRepository.get(resultName));
log.info("Result {} already exists.", resultName);
}
}
);
return Collections.unmodifiableList(resultList);
}
Run Code Online (Sandbox Code Playgroud)
有人能说出我做错了什么吗?
throws关键字仅用于检查的异常。它通过throws关键字指示调用者使用try catch块来排除所有列出的异常。
由于我们知道模块中可能发生哪种检查异常,因此:
这样,我们无需在每次调用该方法时手动手动例外。
我知道以下代码确实有意义:
try { ... }
catch (FileNotFoundException exc) { ... }
catch (IOException exc) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,在throws子句中声明那些父项和子项异常有意义吗?
假设我有以下代码:
public void doSomething() throws FileNotFoundException, IOException { ... }
Run Code Online (Sandbox Code Playgroud)
众所周知,这FileNotFoundException是的子类IOException。现在以任何方式(可读性,性能等)有意义地声明它,而恰恰与此相反:
public void doSomething() throws IOException { ... }
Run Code Online (Sandbox Code Playgroud) 我正在尝试HandleException一种可以处理各种异常的方法。
问题是,我的函数返回一个值。但是,如果我HandleException在 catch 块中使用,Java 会抱怨该函数不返回值,即使我的 HandleException 总是抛出异常。
什么是解决这个问题的好方法?谢谢!
这是一个示例代码。
public class MyException {
static int foo(int num) throws Exception {
try {
return bar(num);
} catch (Exception e) {
handleException();
// throw new Exception("Exception in foo", e);
}
}
static int bar(int num) throws IllegalArgumentException {
if (num < 0) {
throw new IllegalArgumentException("Num less than 0");
}
return num;
}
static void handleException(Exception e) throws Exception {
System.err.println("Handling Exception: " + e);
throw new Exception(e); …Run Code Online (Sandbox Code Playgroud) 我知道方法签名包括方法名称及其参数列表。
但是throws Exception呢?
public List<ServiceStatusVo> listServiceStatuses() throws RetrieverException {
...
return list;
}
Run Code Online (Sandbox Code Playgroud)
如果不包括在内,那么为什么我不能传入以下 lambda:
() -> listServiceStatuses()
Run Code Online (Sandbox Code Playgroud)
但我可以通过
() -> {
try {
return listServiceStatuses();
} catch (RetrieverException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
我也可以再次扔掉它
() -> {
try {
return listServiceStatuses();
} catch (RetrieverException e) {
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Supplier<T>函数式接口,如果 throws不是方法签名的一部分,那真的让我感到困惑。
谢谢您的帮助。