Topic: JAVA的异常[求助]

  Print this page

1.JAVA的异常[求助] Copy to clipboard
Posted by: gao627
Posted on: 2005-05-11 22:43

JAVA中方法可以用throws来声明可能会抛出的异常,对大多数Exception子类来说,Java编译器会强迫你声明在一个成员函数中抛出的“异常”的类型。如果“异常”的类型是Error或RuntimeException,或它们的子类,这个规则不起作用。如果你想明确地抛出一个RuntimeException,你必须用throws语句来声明它的类型。有点不明白,代码如下:
class text
{
public static void main(String []args)
{
    text();
}
static void text() throws ArithmeticException //声明抛出算术异常
{ }
}
这段代码我在调用main()中调用text()并未用try-catch处理,但是编译却可以通过,请问是为什么?

同样的
class text
{
public static void main(String []args)
{
    text();
}
static void text()
{
throw new ArithmeticException();//抛出一个ArithmeticException实例
}
}
我在text方法定义中没有用throws声明,但却可以直接抛出异常实例又是为什么?这样的话throws对RuntimeException的意义在哪里?

2.Re:JAVA的异常[求助] [Re: gao627] Copy to clipboard
Posted by: gao627
Posted on: 2005-05-12 22:12

原因我已经了解了,但是还不是清楚throws对例如ArithmeticException等异常 的意义,请高人指教.

3.Re:JAVA的异常[求助] [Re: gao627] Copy to clipboard
Posted by: coucoo
Posted on: 2005-05-14 10:51

嗯,我也等着看结果

4.Re:JAVA的异常[求助] [Re: gao627] Copy to clipboard
Posted by: ftang
Posted on: 2005-05-14 13:09

ok, your question could be seperated 2 part:

1. why u didn't declare try...catch in main, but compiler didn't force you to do that?
...because the main method in JVM have special meaning and there is special handling for that...let me check the official articles and give you more detail later...

2. when use try...catch and when use throws?
we can consider there are 2 type of exceptions:
a. user didn't provide enough information/data make your process have to halt
we call this "broken contract"
when this type of broken contract error happen , you should throw the
exception back, let user handle this...which he should.
b. your process have it own problem. you can try to catch...and "fix" it...

3. checked and unchecked exception
RuntimeException and Error is unchecked Exception--also arithmatic exception, your question I guess is if they are unchecked exception why/when throw them?
well this question should start like this:
why sun seperate exception into 2 catalog: checked and unchecked?
when sun design the exception handling their thought is this:
"If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception"
so obviously the RuntimeException is type of Exception that Sun think the client can NOT handle, so make it as Unchecked Exception...and there is another reason behind this is because: Exception Handling is expensive!!! that is the real real reason SUN create checked and unchecked Exception...and but this not means you can't handle the unchecked exception. the truth is SUN want you to make that decision...if is a unchecked exception... you can handle it but question is "is it worth"?

I don't know if this is clear to answer your question...if you still have problem, let me know...

5.Re:JAVA的异常[求助] [Re: gao627] Copy to clipboard
Posted by: dengyinjun
Posted on: 2005-05-25 03:00

throw 抛出一个异常对象~~~~~~~~


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923