2个问题都是关于异常的。
问题1:关于异常有句话不是很明白,“要么处理这个异常,要么就在异常说明中表明此方法将产生异常。”
class SimpleException extends Exception {}public class InheritingExceptions {
public void f(){ //为什么这里非要写 throws 语句,万一抛出的异常不是指定的类型呢?
//public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args) {
InheritingExceptions sed = new InheritingExceptions();
try {
sed.f();
} catch(SimpleException e) {
System.out.println("Caught it!");
}
}
}问题2:
public static void h() throws Exception {
try {
f();
} catch(Exception e) {
System.out.println("Inside h(),e.printStackTrace()");
e.printStackTrace(System.out);
throw (Exception)e.fillInStackTrace(); //问题2:这里为什么要类型转换成Exception,这里本来不就是Exception类型的吗?
}
}
问题1:关于异常有句话不是很明白,“要么处理这个异常,要么就在异常说明中表明此方法将产生异常。”
class SimpleException extends Exception {}public class InheritingExceptions {
public void f(){ //为什么这里非要写 throws 语句,万一抛出的异常不是指定的类型呢?
//public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args) {
InheritingExceptions sed = new InheritingExceptions();
try {
sed.f();
} catch(SimpleException e) {
System.out.println("Caught it!");
}
}
}问题2:
public static void h() throws Exception {
try {
f();
} catch(Exception e) {
System.out.println("Inside h(),e.printStackTrace()");
e.printStackTrace(System.out);
throw (Exception)e.fillInStackTrace(); //问题2:这里为什么要类型转换成Exception,这里本来不就是Exception类型的吗?
}
}
解决方案 »
- Netbeans中能否实现:一个用各种现有布局如GridLayout、FlowLayout写好的图形界面的程序,在NetBeans中能看到并可视化微调大小、位置
- 一个java小问题,请给个答案
- 一个FocusListener的问题
- 急!急!java在web实现对网络拓扑图的编辑
- sql server driver for jdbc 问题
- 关于JTree组件控制上的问题,望jave界面设计高手帮忙解答
- 阿里巴巴面试题的java初始化问题
- 散分!!在500万条记录中定位ResultSet中的一条
- intelliJ IDEA 2.6的汉字半角问题
- 怎样才能开一个新的子论坛?
- iReport2.05 版本中怎么实现边框
- 这个正则表达式的Pattern如何写?
因为只有你说明了抛出什么异常,调用者才知道如何处理,并在调用方法的外面使用正确的try catch来捕获这个异常.
要么你在方法内部直接try catch处理异常,如果你不想处理异常,就抛出它,让外面调用这个方法的地方处理.
---> Java的异常为了使程序更健壮,你下面的main函数里想要捕捉这个异常 如果你不抛出 怎么捕获?
//问题2:这里为什么要类型转换成Exception,这里本来不就是Exception类型的吗?
---> 我查了下e.fillInStackTrace(); 返回的Throwable类 是Exception的父类 是不是用强制转换成子类Exception
再做处理
//public void f(){ //为什么这里非要写 throws 语句,万一抛出的异常不是指定的类型呢?
public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args) {
InheritingExceptions sed = new InheritingExceptions();
try {
sed.f();
} catch(SimpleException e) {
System.out.println("Caught it!");
}
}
}
因为有红色这句存在 所以你必须在函数外抛出异常 否则你就必须现在就处理这个排除的异常
如果你在f()里不抛出异常 因为绿色语句的存在 就会有错误了总之 你要抛出异常 就得处理 要不就抛出 但总要处理的;另一方面 你要处理 就得有异常去捕获再处理
你可能会知道产生什么异常,但是你不知道怎么处理这个异常,或者是不想处理这个异常,而让其他的类去处理
那个try(){}catch{}块,主要是保证程序真的出现异常时,不会终止,当然也可以在其中处理一些释放资源之类的事情
有的是给用户看的,就比如没有填写正确格式的邮箱,就会捕获到自己定义的异常,给用户提示到页面。
如果你写段根据用户输入的数字进行除法的代码。用户如果输入了10/0,你能一早知道么?这是运行时异常。try/catch是处理,throws也是处理(交由低层代码处理),怎么选用取决与架构设计。
class SimpleException extends Exception {}public class InheritingExceptions {
//public void f(){ //为什么这里非要写 throws 语句,万一抛出的异常不是指定的类型呢?
public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args) {
InheritingExceptions sed = new InheritingExceptions();
try {
sed.f();
} catch(SimpleException e) {
System.out.println("Caught it!");
}
}
}
对啊 让你明白异常抛出处理流程的啊但是在Java编程过程中你会碰到受控异常和非受控异常 受控异常是你必须处理的 还要尽量处理非受控异常
http://blog.csdn.net/justinavril/archive/2008/08/03/2761119.aspx
class SimpleException extends Exception {}public class InheritingExceptions {
public void f() throws NoSuchFieldException{ //现在实际产生的异常,和我预想产生的异常不一样了,为什么还是不行?
//总不能说,我预想的异常就得和实际产生的异常一样吧?
//public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args) {
InheritingExceptions sed = new InheritingExceptions();
try {
sed.f();
} catch(Exception e) { //这里也修改了
System.out.println("Caught it!");
}
}
}
2或者在方法上面声明异常,不做处理(其它类引用这个方法时候,要处理这个异常的,呵呵)
3要不处理掉异常(用try{...}cathc(Exception){...})
}
这样就可以捕获任何的异常了,不过尽量不要由它来捕获异常.
是啊,所以下面的catch我特地改成 Exception来做试验,可是还是不行~~~