我总是在错误的背景下看到Throwable/Exception.但是我可以想到扩展一个Throwablejust来打破一堆递归方法调用会非常好的情况.比如说,您试图通过递归搜索的方式在树中查找并返回一些对象.一旦你发现它粘在一些Carrier extends Throwable并抛出它,并在调用递归方法的方法中捕获它.
正面:您不必担心递归调用的返回逻辑; 既然你找到了你需要的东西,为什么还要担心如何将这个引用备份到方法堆栈中.
否定:您有一个不需要的堆栈跟踪.该try/catch块也变得违反直觉.
这是一个愚蠢的简单用法:
public class ThrowableDriver {
public static void main(String[] args) {
ThrowableTester tt = new ThrowableTester();
try {
tt.rec();
} catch (TestThrowable e) {
System.out.print("All good\n");
}
}
}
public class TestThrowable extends Throwable {
}
public class ThrowableTester {
int i=0;
void rec() throws TestThrowable {
if(i == 10) throw new TestThrowable();
i++;
rec();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,是否有更好的方法来达到同样的目的?另外,以这种方式做事有什么不妥之处吗?
所以Java在Throwable中有一个方法
public final void addSuppressed(Throwable exception)
Run Code Online (Sandbox Code Playgroud)
这就是它的作用:
将指定的异常附加到为了传递此异常而被抑制的异常.此方法是线程安全的,通常由try-with-resources语句调用(自动和隐式).
..我对此感到困惑,什么是"为了传递此异常而被抑制的异常的指定异常".?
假设我有我的 custom RuntimeException,MyEntityJPA在哪里@Entity:
@Getter
public class MyEntityAlreadyExistingException extends RuntimeException {
private final MyEntity myEntity;
public MyEntityAlreadyExistingException(MyEntity myEntity) {
super(MessageFormat.format("MyEntity with name \"{0}\" already exists", myEntity.getName()));
this.myEntity = myEntity;
}
}
Run Code Online (Sandbox Code Playgroud)
声纳提示我进行myEntity瞬态或可序列化。
我应该如何处理这种情况?
我不使用任何 RMI,也不使用任何远程处理。它是一个相对简单的 Spring Boot Web 应用程序,带有 JPA。
如果我可以myEntity序列化,我以后可以利用哪些优势?
我认为Exception.fillInStackTrace应该返回Exception或派生的Exception对象.考虑到以下两个功能,
public static void f() throws Throwable {
try {
throw new Throwable();
} catch (Exception e) {
System.out.println("catch exception e");
e.printStackTrace();
}
}
public static void g() throws Throwable {
try {
try {
throw new Exception("exception");
} catch (Exception e) {
System.out.println("inner exception handler");
throw e.fillInStackTrace();
}
} catch (Exception e) {
System.out.println("outer exception handler");
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
exception handler没能赶上new Throwable()第一功能f().exception handler可以赶上e.fillInstackTrace()第二功能g().g()仍然需要 …请告诉我以下情况之间的区别:
public class Test {
private static < T extends Throwable > void doThrow(Throwable ex) throws T {
throw (T) ex;
}
public static void main(String[] args) {
doThrow(new Exception()); //it's ok
}
}
Run Code Online (Sandbox Code Playgroud)
这种情况下没有编译错误
和
public class Test {
private static < T extends Throwable > void doThrow(Throwable ex) throws Throwable {
throw (T) ex;
}
public static void main(String[] args) {
doThrow(new Exception()); //unhandled exception
}
}
Run Code Online (Sandbox Code Playgroud)
有编译错误
API doc说从不捕获Throwable子类Error表示异常行为.这是否意味着Error和Exception之间的隔离是告诉程序员应该捕获哪个子类,哪个不应该?或者还有更多呢?
举个这样的例子:
public List<CloseableThing> readThings(List<File> files) throws IOException {
ImmutableList.Builder<CloseableThing> things = ImmutableList.builder();
try {
for (File file : files) {
things.add(readThing(file))
}
return things.build();
} catch (Throwable t) {
for (CloseableThing thing : things.build()) {
thing.close();
}
throw t;
}
}
Run Code Online (Sandbox Code Playgroud)
代码审查评论的出现是因为通常有一条规则不会捕获Throwable.进行此类仅故障清理的旧模式是:
public List<CloseableThing> readThings(List<File> files) throws IOException {
ImmutableList.Builder<CloseableThing> things = ImmutableList.builder();
boolean success = false;
try {
for (File file : files) {
things.add(readThing(file))
}
success = true;
return things.build();
} finally {
if (!success) {
for (CloseableThing …Run Code Online (Sandbox Code Playgroud) 我抓到了一个Throwable错误.
catch (Throwable t){
System.out.println(t.getCause().getMessage());
}
Run Code Online (Sandbox Code Playgroud)
当我在一行上放置断点System.out.并将鼠标悬停在(Eclipse)t变量上时,Eclipse会向我显示原因:java.lang.NoClassDefFoundError: myclass
但是当我发布我正在null寻找的断点时t.getCause().
为什么会这样?我怎样才能得到原因字符串.
更新:
如果我抓住的话也会发生
catch (NoClassDefFoundError e){
}
Run Code Online (Sandbox Code Playgroud) 我有无处不在的HelloWorldApp.java文件
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Run Code Online (Sandbox Code Playgroud)
我跑:
javac HelloWorldApp.java
Run Code Online (Sandbox Code Playgroud)
然后我跑:
jdb HelloWorldApp
Run Code Online (Sandbox Code Playgroud)
我明白了:
Initializing jdb ...
>
Run Code Online (Sandbox Code Playgroud)
我键入:
stop at HelloWorldApp.main:7
Run Code Online (Sandbox Code Playgroud)
在哪里提示
然后我明白了
Deferring breakpoint HelloWorldApp.main:7.
It will be set after the class is loaded.
>
Run Code Online (Sandbox Code Playgroud)
我键入:
run
Run Code Online (Sandbox Code Playgroud)
在哪里提示
然后我明白了
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Hello World!
The application …Run Code Online (Sandbox Code Playgroud) 我目前正在研究一个库存管理系统。在系统中有注册选项,如下所示:
以下代码显示了注册按钮方法:
btnRegister.setOnAction(e ->{// register button method
try {
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("Register.fxml").openStream());
RegisterController Msg = loader.getController();
Scene scene = new Scene(root);
scene.getStylesheets().setAll(
getClass().getResource("style.css").toExternalForm()
);
stage.setScene(scene);
stage.show();
((Node) e.getSource()).getScene().getWindow().hide();
} catch (Exception ex) {
System.out.println(ex);
}
});Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
Warning:(64, 36) Throwable argument 'ex' to 'System.out.println()' call
Run Code Online (Sandbox Code Playgroud)
有人可以解释这个警告以及解决这个问题的可能方法吗?
如果被忽略,这会对以后的新更改产生任何影响吗?
它专门抱怨 register 方法中的以下代码行,其中异常是:
} catch (Exception ex) {
System.out.println(ex);
}
Run Code Online (Sandbox Code Playgroud)