我知道必须处理或指定已检查的异常,但未经检查的异常是可选的.
如果由于某种原因我可以合理地预期在方法中发生未经检查的异常,我应该将它添加到throws规范吗?或者我应该尽可能缩短规格?
filecontent.java:15:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出
演出文件(); ^ filecontent.java:78:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出
演出文件(); ^
我已经抛出了java.io.IOException,但它仍然显示了这些错误.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class filecontent extends Frame implements ActionListener
{
TextField t[] = new TextField[4];
TextArea ta[] = new TextArea[4];
Button submit;
Panel p1;
filecontent()
{
setGUI();
setRegister();
showfile();
setTitle("FileData");
setVisible(true);
setSize(300, 300);
/* addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
*/
}
void setGUI()
{
p1 = new Panel();
p1.setLayout(new GridLayout(5, 4, 10, 10));
for(int i=0; i<4; i++)
{
t[i] = …Run Code Online (Sandbox Code Playgroud) 我在变量中有一个异常(没有抛出).
什么是最好的选择?
Exception exception = someObj.getExcp();
try {
throw exception;
} catch (ExceptionExample1 e) {
e.getSomeCustomViolations();
} catch (ExceptionExample2 e) {
e.getSomeOtherCustomViolations();
}
Run Code Online (Sandbox Code Playgroud)
要么
Exception exception = someObj.getExcp();
if (exception instanceof ExceptionExample1) {
exception.getSomeCustomViolations();
} else if (exception instanceof ExceptionExample2) {
exception.getSomeOtherCustomViolations();
}
Run Code Online (Sandbox Code Playgroud) 我从来没有使用过"throws"子句,今天一个配偶告诉我,我必须在方法声明中指定方法可能抛出的异常.但是,如果不这样做,我一直在使用异常而没有问题,所以,如果事实上它需要它,为什么还需要呢?
我有一个抛出错误的函数,在这个函数中我有一个inside a闭包,我需要从它的完成处理程序抛出错误.那可能吗 ?
到目前为止,这是我的代码.
enum CalendarEventError: ErrorType {
case UnAuthorized
case AccessDenied
case Failed
}
func insertEventToDefaultCalendar(event :EKEvent) throws {
let eventStore = EKEventStore()
switch EKEventStore.authorizationStatusForEntityType(.Event) {
case .Authorized:
do {
try insertEvent(eventStore, event: event)
} catch {
throw CalendarEventError.Failed
}
case .Denied:
throw CalendarEventError.AccessDenied
case .NotDetermined:
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
if granted {
//insertEvent(eventStore)
} else {
//throw CalendarEventError.AccessDenied
}
})
default:
}
}
Run Code Online (Sandbox Code Playgroud) 我正在用Java编写程序,几乎我的一个类中的每个方法都写成:
public void doStuff() throws AWTException{}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我摆脱为每个方法输入抛出AWTException的额外步骤,并以某种方式为整个类做它?
在我的方法之一中,中断异常和执行异常即将到来。我像这样输入了 try catch 。
try{
//my code
}catch(InterruptedException|ExecutionException e)
Log.error(" logging it");
throw new MonitoringException("it failed" , e)
//monitoringexception extends RunTimeException
Run Code Online (Sandbox Code Playgroud)
同样在我的方法中,我抛出了 InterruptedException,ExecutionException
我在声纳中遇到严重错误 - 重新中断此方法或重新抛出“ InterruptedException”
有人知道怎么修这个东西吗。
请立即帮助。
我的Parent班级是:
import java.io.IOException;
public class Parent {
int x = 0;
public int getX() throws IOException{
if(x<=0){
throw new IOException();
}
return x;
}
}
Run Code Online (Sandbox Code Playgroud)
我extend这个类写一个子类Child:
public class Child1 extends Parent{
public int getX(){
return x+10;
}
}
Run Code Online (Sandbox Code Playgroud)
注意在覆盖类中的getX方法时Child,我已从throws方法定义中删除了该子句.现在,它会导致编译器出现异常行为,这是预期的:
new Parent().getX() ;
Run Code Online (Sandbox Code Playgroud)
如果不try-catch按预期将其封闭在块中,则不会进行编译.
new Child().getX() ;
Run Code Online (Sandbox Code Playgroud)
编译而不将其封闭在一个try-catch块中.
但是下面的代码行需要try-catch块.
Parent p = new Child();
p.getX();
Run Code Online (Sandbox Code Playgroud)
正如可以预见的那样,即在运行时多态性期间使用父类引用来调用子方法,为什么Java的设计者没有强制要求在覆盖特定父类方法时在方法定义中包含throws子句?我的意思是如果父类方法在其定义中有throws子句,那么在覆盖它时,重写方法还应该包括throws子句,不是吗?
我正在构建一个有很多计算的科学软件,当然参数可能有不正确的长度等等......所以我使用了IllegalArgumentException类,因为它似乎是问题的正确名称,但是我应该把它throws IllegalArgumentException放在函数定义中吗?
我问这个是因为在我写完之后,Eclipse编辑器没有让我用try和catch来包围这个函数.我认为这是如何实施尝试和捕获.我已经阅读了Java.com上的异常处理教程,但我不确定我是否理解了关于我的问题的部分.
一些背景,然后一些问题.
我最近才发现接口(或类)在其方法可能引发的(已检查)异常类型中可能是通用的.例如:
interface GenericRunnable<X extends Exception> {
void run() throws X;
}
Run Code Online (Sandbox Code Playgroud)
关键是如果您稍后使用,例如IOException并调用该run方法来实例化它,则编译器知道您需要捕获IOException或将其标记为抛出.更好的是,如果X是a RuntimeException,你根本不需要处理它.
这是一个使用上述界面的人为例子,但它基本上是一个回调,应该很常见.
public <X extends Exception> void runTwice(GenericRunnable<X> runnable) throws X {
runnable.run(); runnable.run();
}
...
public myMethod() throws MyException {
runTwice(myRunnable);
}
Run Code Online (Sandbox Code Playgroud)
我们正在调用一个通用的实用程序方法runTwice(可能在外部库中定义)来运行我们自己的特定方法并使用特定的检查异常,并且我们不会丢失任何有关可能抛出哪个特定的已检查异常的信息.
另一种方法将是简单地使用throws Exception双方的Runnable.run方法和runTwice方法.这不会限制Runnable接口的任何实现,但是检查异常的优点将会丢失.或者根本就没有throws,也失去了检查异常的优势,并可能迫使实现包装.
因为我从未见过throws X,也许我错过了什么.此外,我已经看到回调示例多次用作针对已检查异常的参数,而不会被反驳.(这个问题对检查异常的优缺点不感兴趣.)
是throws X通常一个好主意?优缺点都有什么?你能给出一些使用throws X或不使用但应该具有的例子吗?
基本上,我想进一步了解一下.您可以评论以下示例.
OutputStream抛出IOException(也许ByteArrayOutputStream可以延伸GenericOutputStream<RuntimeException>) …
throws ×10
java ×9
exception ×5
generics ×1
inheritance ×1
instanceof ×1
ioexception ×1
ios ×1
overriding ×1
performance ×1
polymorphism ×1
swift2 ×1
try-catch ×1