我有一些代码可能会抛出已检查和运行时异常.
我想捕获已检查的异常并将其包装为运行时异常.但是如果抛出RuntimeException,我不必将它包装起来,因为它已经是运行时异常.
我的解决方案有点开销,并不"整洁":
try {
// some code that can throw both checked and runtime exception
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
想要更优雅的方式吗?
什么是运行时异常以及什么是已检查/未检查异常以及错误/异常之间的区别.为什么这么多类型?相反,Java可能只是遵循一个简单的设计(只是尝试/捕获所有类型)来处理程序中的异常情况?
java exception-handling exception runtimeexception checked-exceptions
我是Java的新手,也是编程的新手(我知道直接进入Java可能不是最好的主意.)而且无论我如何尝试在程序中添加暂停,我都会遇到错误.我正在做一个简单的计数程序,并希望在每个数字之间添加一秒延迟,这是我到目前为止的代码:
import java.lang.*;
public class Counter
{
public static void main(String[]args)
{
int i;
for (i = 0; i <= 10; i++)
{
Thread.sleep(1000);
System.out.println(i);
}
System.out.println("You can count to ten.");
}
}
Run Code Online (Sandbox Code Playgroud)
调用Thread.sleep()不会编译.该javac编译器说:"没有报告异常InterruptedException异常;必须捕获或声明抛出"和Eclipse说,"未处理的异常类型InterruptedException的"
我正在重构一些使用番石榴缓存的代码.
初始代码:
public Post getPost(Integer key) throws SQLException, IOException {
return PostsDB.findPostByID(key);
}
Run Code Online (Sandbox Code Playgroud)
为了不破坏某些东西,我需要保留任何抛出的异常,而不包装它.
目前的解决方案看起来有些难看
public Post getPost(final Integer key) throws SQLException, IOException {
try {
return cache.get(key, new Callable<Post>() {
@Override
public Post call() throws Exception {
return PostsDB.findPostByID(key);
}
});
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SQLException) {
throw (SQLException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) …Run Code Online (Sandbox Code Playgroud) 我有这样的方法:
public void getSomething(){
...
}
Run Code Online (Sandbox Code Playgroud)
我想扔一个Exception内心getSomething().编译器不允许我这样做,因为我的方法不允许Exception在那里抛出.但我需要Exception为我的测试抛出一个子类(我不能抛出Unchecked Exception).这显然是一个黑客,但我需要它进行测试.我试过EasyMock,但它也不允许我这样做.任何想法如何做到这一点?
谢谢Sean Nguyen
I am wondering why the java compiler allows throws in the method declaration when the method will never throw the Exception. Because "throws" is a way of handling the exception (telling the caller to handle it).
Since there are two ways of handling exception (throws & try/catch). In a try/catch, it doesn't allow the catch of an exception not thrown in the try block but it allows a throws in a method that does may not throw the exception.
private …Run Code Online (Sandbox Code Playgroud) 我处理具有遗留服务层的项目,如果请求的记录不存在,则在许多地方返回null,或者由于呼叫者未被授权而无法访问.我在谈论ID要求的特定记录.例如,类似于:
UserService.get(userId);
Run Code Online (Sandbox Code Playgroud)
我最近推动改变这个API,或者补充一个抛出异常的新API.关于已检查与未经检查的例外的争论随之而来.
从JPA/Hibernate等人的设计者那里得到一个说明,我建议未经检查的异常可能是最合适的.我的论点是,无法合理地期望API的用户从这些异常中恢复,并且在99%的情况下,我们最多只能通知应用程序用户发生了一些错误.
将运行时异常传播到通用处理机制显然会减少处理边缘情况异常所涉及的许多复杂性和所需的分支处理.但是,围绕这种方法存在很多担忧(这是正确的).
为什么选择JPA/EJB和Hibernate等项目的设计者使用未经检查的异常模型?它有充分的理由吗?有什么利弊.使用这些框架的开发人员是否仍然可以使用适配器包装器之类的东西处理接近它们抛出位置的运行时异常?
我希望这些问题的答案可以帮助我们对自己的服务层做出"正确"的决定.
我偶然注意到这个throw语句(从一些更复杂的代码中提取)编译:
void foo() {
try {
} catch (Throwable t) {
throw t;
}
}
Run Code Online (Sandbox Code Playgroud)
对于一个短暂而快乐的时刻,我认为已经检查过的异常最终决定已经死了,但它仍然在这方面很高兴:
void foo() {
try {
} catch (Throwable t) {
Throwable t1 = t;
throw t1;
}
}
Run Code Online (Sandbox Code Playgroud)
该try块不必为空; 它似乎可以有代码,只要该代码不会抛出已检查的异常.这似乎是合理的,但我的问题是,语言规范中的哪些规则描述了这种行为?据我所知,§14.18town语句明确禁止它,因为t表达式的类型是一个已检查的异常,并且它没有被捕获或声明被抛出.(?)
在阅读关于异常的同时,我总会遇到检查异常和未经检查的异常,所以想知道如何区分哪个是什么?
编辑:我想知道我是否创建了任何异常类,那么我如何创建一个已选中或未选中?
每个人的意义是什么?
java exception-handling checked-exceptions unchecked-exception
我有一些丑陋的代码,想要重构它:
public class UdpTransport extends AbstractLayer<byte[]> {
private final DatagramSocket socket;
private final InetAddress address;
private final int port;
/* boolean dead is provided by superclass */
public UdpTransport(String host, int port) {
this.port = port;
InetAddress tmp_address = null;
try {
tmp_address = InetAddress.getByName(host);
} catch (UnknownHostException e) {
e.printStackTrace();
dead = true;
socket = null;
address = null;
return;
}
address = tmp_address;
DatagramSocket tmp_socket = null;
try {
tmp_socket = new DatagramSocket();
} catch (SocketException e) { …Run Code Online (Sandbox Code Playgroud)