Java 抛出一个不带“quot”的异常;线程中的异常…“;
我想知道是否有一种简单的抛出异常的方法,但是只能使用我选择的字符串。我找到了一种摆脱堆栈跟踪的方法,但现在我想删除每个异常的开头: 线程“main”运行时异常中的异常 我正在寻找一种简单、优雅的方法来实现这一点(不是超级简单,但也不是太复杂)Java 抛出一个不带“quot”的异常;线程中的异常…“;,java,exception,runtimeexception,throwable,Java,Exception,Runtimeexception,Throwable,我想知道是否有一种简单的抛出异常的方法,但是只能使用我选择的字符串。我找到了一种摆脱堆栈跟踪的方法,但现在我想删除每个异常的开头: 线程“main”运行时异常中的异常 我正在寻找一种简单、优雅的方法来实现这一点(不是超级简单,但也不是太复杂) 谢谢 这就是你的做法: try{ //your code }catch(Exception e){ System.out.println("Whatever you want to print" + e.getMessage()); S
谢谢 这就是你的做法:
try{
//your code
}catch(Exception e){
System.out.println("Whatever you want to print" + e.getMessage());
System.exit(0);
}
这就是你如何做到的:
try{
//your code
}catch(Exception e){
System.out.println("Whatever you want to print" + e.getMessage());
System.exit(0);
}
构造异常对象时,其中一个构造函数将获取消息的字符串对象。构造异常对象时,其中一个构造函数将获取消息的字符串对象。除非获得openJDK,否则无法更改源并重新编译 然而,大多数开发人员通常使用一些日志库,如log4j,并根据日志设置使用不同的详细级别
因此,您可以使用较低的级别(如跟踪或调试)打印完整的stacktrace,并在错误或警告(甚至信息)级别打印更具可读性的消息。除非获得openJDK,否则无法更改源代码并重新编译 然而,大多数开发人员通常使用一些日志库,如log4j,并根据日志设置使用不同的详细级别 因此,您可以使用较低级别(如跟踪或调试)打印完整的stacktrace,并在错误或警告(甚至信息)级别打印更易于阅读的消息。只需执行以下操作:
try {
...
} catch (Exception e) {
System.err.print("what ever");
System.exit(1); // close the program
}
只要做:
try {
...
} catch (Exception e) {
System.err.print("what ever");
System.exit(1); // close the program
}
我不确定我是否完全理解您的问题,但是如果您只是将“throws Exception”添加到方法头中,并将异常抛出到应该失败的方法中,那么这应该是可行的 例如:
public void HelloWorld throws Exception{
if(//condition that causes failure)
throw new Exception("Custom Error Message");
else{
//other stuff...
}
}
我不确定我是否完全理解您的问题,但是如果您只是将“throws Exception”添加到方法头中,并将异常抛出到应该失败的方法中,那么这应该是可行的 例如:
public void HelloWorld throws Exception{
if(//condition that causes failure)
throw new Exception("Custom Error Message");
else{
//other stuff...
}
}
正确的方法是设置自己的、自定义的
正确的方法是设置自己的、自定义的
您可以通过创建自定义的
异常来实现这一点,您可以自己创建该异常
- 这些异常可以是
检查的异常,由Java编译器强制执行(需要try/catch或throws来实现)
- 或者异常可以是
未选中的
异常,它在运行时抛出,Java编译器不强制执行
根据您所写的内容,您似乎想要一个未选中的
异常,该异常不强制执行,但在运行时抛出一个错误
一种方法是通过以下方式:
public class CustomException extends RuntimeException {
CustomException() {
super("Runtime exception: problem is..."); // Throws this error message if no message was specified.
}
CustomException(String errorMessage) {
super(errorMessage); // Write your own error message using throw new CustomException("There was a problem. This is a custom error message");
}
}
public class Example {
String name = "x";
if(name.equals("x"))
throw new CustomException(); // refers to CustomException()
}
然后在代码中,可以执行以下操作:
public class CustomException extends RuntimeException {
CustomException() {
super("Runtime exception: problem is..."); // Throws this error message if no message was specified.
}
CustomException(String errorMessage) {
super(errorMessage); // Write your own error message using throw new CustomException("There was a problem. This is a custom error message");
}
}
public class Example {
String name = "x";
if(name.equals("x"))
throw new CustomException(); // refers to CustomException()
}
或
您也可以对Throwable执行此操作。您可以通过创建自定义的
异常来执行此操作,该异常可以由您自己创建
- 这些异常可以是
检查的异常,由Java编译器强制执行(需要try/catch或throws来实现)
- 或者异常可以是
未选中的
异常,它在运行时抛出,Java编译器不强制执行
根据您所写的内容,您似乎想要一个未选中的
异常,该异常不强制执行,但在运行时抛出一个错误
一种方法是通过以下方式:
public class CustomException extends RuntimeException {
CustomException() {
super("Runtime exception: problem is..."); // Throws this error message if no message was specified.
}
CustomException(String errorMessage) {
super(errorMessage); // Write your own error message using throw new CustomException("There was a problem. This is a custom error message");
}
}
public class Example {
String name = "x";
if(name.equals("x"))
throw new CustomException(); // refers to CustomException()
}
然后在代码中,可以执行以下操作:
public class CustomException extends RuntimeException {
CustomException() {
super("Runtime exception: problem is..."); // Throws this error message if no message was specified.
}
CustomException(String errorMessage) {
super(errorMessage); // Write your own error message using throw new CustomException("There was a problem. This is a custom error message");
}
}
public class Example {
String name = "x";
if(name.equals("x"))
throw new CustomException(); // refers to CustomException()
}
或
你也可以为一次性物品这样做。这有什么特别的原因吗?嗯。。。对我来说似乎有点多余,因为你要求的只是
try{…}catch(Exception e){System.err.print(“what ever”);}
没有特别的原因,我只是想知道这样的事情是否可能以及如何实现。目标是通过抛出异常而不是打印简单的字符串来崩溃程序。这有什么特别的原因吗?嗯。。。对我来说似乎有点多余,因为你要求的只是try{…}catch(Exception e){System.err.print(“what ever”);}
没有特别的原因,我只是想知道这样的事情是否可能以及如何实现。目标是通过抛出异常而不是打印出简单的字符串来崩溃程序。这是一个简单的打印输出。。。我想通过抛出一个异常来停止程序。比如在catch子句中添加System.exit?这是一个简单的打印输出。。。我想通过抛出异常来停止程序。例如在catch子句中添加System.exit?为什么我忘记了System.err.print()(谢谢!为什么我忘了System.err.print()?:(谢谢,不会抛出新的IllegalArgumentException(“再见,世界!”)
print线程中的异常…
这正是op试图避免的…但您对正确的方法100%正确。@DimaAligin试试看。@edle自由地重新接受答案…这是正确的方法。这是正确的方法。如果我将其更改为System.err.print(e.getMessage());这应该正是我想要的。这也避免了堆栈跟踪,这很好。谢谢。不会抛出新的IllegalArgumentException(“再见,世界!”)
print线程中的异常…
这正是op试图避免的…但您对正确的方法100%正确。@DimaAligin试试看。@edle自由地重新接受答案…这是正确的方法。这是正确的方法。如果我将其更改为System.err.print(e.getMessage());这应该正是我想要的。这也避免了堆栈跟踪,这很好。谢谢!这是非常基本的异常处理…我试图避免堆栈跟踪和每个异常文本的开头。这是非常基本的异常处理…我试图避免堆栈跟踪和每个异常文本的开头。