l**b 发帖数: 457 | 1 个人感觉,你的app需要处理所有的exception的时候,你可以try catch Throwable,
还有就是你要自己写unchecked exception的时候也要继承Throwable。其他就没什么用
了,一般写library的会用到比较多,如果是写application的,一般很伤会用到
throwable,但是我们在写一些service什么的时候会习惯catch 所有的Throwable然后
log一下看看是什么。 |
|
p***p 发帖数: 559 | 2 3ks ... one more question, getMessage and StackTrace, what is the difference
A cause can be associated with a throwable in two ways: via a constructor
that takes the cause as an argument, or via the initCause(Throwable) method.
New throwable classes that wish to allow causes to be associated with them
should provide constructors that take a cause and delegate (perhaps
indirectly) to one of the Throwable constructors that takes a cause. For
example:
try {
lowLevelOp();
} catch |
|
p***p 发帖数: 559 | 3 3ks ... one more question, getMessage and StackTrace, what is the difference
A cause can be associated with a throwable in two ways: via a constructor
that takes the cause as an argument, or via the initCause(Throwable) method.
New throwable classes that wish to allow causes to be associated with them
should provide constructors that take a cause and delegate (perhaps
indirectly) to one of the Throwable constructors that takes a cause. For
example:
try {
lowLevelOp();
} catch |
|
s******e 发帖数: 493 | 4 jsp has implicit exception object if the jsp page is error page
Throwable exception = (Throwable)request.getAttribute("javax.servlet.error.
exception");
if you try to get it in servlet or the page is not error page. this works
only for spec2.3+
Throwable exception = (Throwable) request.getAttribute("javax.servlet.jsp.
jspException");
should be used for 2.3- |
|
t******t 发帖数: 51 | 5 I installed Oc4j as a standalone package in a directory (Solaris2.6) where
everybody in my group has full access. Now the problem is I'm the only person
who can start the application server by the following:
java -jar oc4j.jar
For other people in the group , they get exceptions like:
java.lang.ClassNotFoundException: oracle.dms.instrument.DMSConsole
at java.lang.Throwable.fillInStackTrace(Native Method)
at java.lang.Throwable.fillInStackTrace(Compiled Code)
at java.lang.Throwable. |
|
p******h 发帖数: 1 | 6 I throw errors in my coding, when I test it, the following info come out. What
do they mean? I used throw new Error("e"). Thanks a lot!!!
at java.lang.Throwable.fillInStackTrace(Native Method)
at java.lang.Throwable.(Throwable.java:94)
at java.lang.Error.(Compiled Code) |
|
|
l**b 发帖数: 457 | 8 head first java吧,然后随便考个SCJP,以后这种都不怕了。
1)需要多种继承关系的时候只能用interface,如果有需要继承到subclass里面的
instance variable的话,就只能用abstract class
2)exception是throwable的subclass,throwable包括了主要2种exception,一个必须
catch的,比如Exception,或者很多类型的IOException都是一定要catch的,另一种是
不一定要catch的,比如ArrayIndexOutOfBound,或者Integer除0的时候的Exception。
具体名字自己查吧。
仅供参考,大牛轻打。 |
|
T****U 发帖数: 3344 | 9 1) abstract class 可以有non-abstract method and variable
2) 只有checked exception才必须declare, 倒不一定catch, 可以throw给下家。具体必
须用throwable的地方还真不多,因为exception可以随便定义的
估计只有不知道调用程序会出来个什么东西时,才需要catch throwable |
|
c*****t 发帖数: 1879 | 10 Catching Throwable is better though. Sometimes class loading
can get you wierd Throwable such as class version error etc.
name |
|
N*n 发帖数: 456 | 11 谢谢。。找了点code在看。。
public class StackTraceExample
{
//private static final Logger logger = Logger.getLogger(StringReplace.class.
getName());
public static void main(String args[])
{
//calling a method to print stack trace further down
first();
}
public static void first()
{
second();
}
private static void second()
{
third();
}
private static void third()
{
//If you want to print stack trace on console than use dumpStack() method
System.err.println("Stack trace of current thread using dumpStack() ... 阅读全帖 |
|
g*****g 发帖数: 34805 | 12 当然不是,你可以catch Throwable,但你得确保你知道你在干什么。所有server 最外
圈都是要catch Throwable的,运行时不能因为一个Error就退出JVM。 |
|
H****S 发帖数: 1359 | 13 我的意思是关闭Lucene index writer,不是说要关掉JVM。
[在 goodbug (好虫) 的大作中提到:]
:当然不是,你可以catch Throwable,但你得确保你知道你在干什么。所有server 最
外圈都是要catch Throwable的,运行时不能因为一个Error就退出JVM。
:
:........... |
|
n*****e 发帖数: 76 | 14 就两java问题,第一个,啥时候用interface,啥时候用abstract class?举例子说明
。第二个,啥时候用throwable,啥时候用exception?两个都不知道怎么回答。
怎么才能快速提高java水平?请教大牛们。。。 |
|
l***i 发帖数: 1309 | 15 signature里面写throw xxxException主要是给caller一个机会handle Exception吧,
其实caller也
可以直接不管然后在自己的signature里面照抄那个Exception就好了.不过看名字感觉
Throwable像个interface阿。java里面还有什么xxxable是class么?
大牛出来指点一下 |
|
p*****2 发帖数: 21240 | 16
明白。就是把Exception更规范话了,虽然代码比较恶心了。Throwable确实听起来像
interface. |
|
l**b 发帖数: 457 | 17 Throwable肯定是interface,所有的exception都可以自己handle,也可以throw给
calling method。这个看你具体的需求吧。
exception是泛指,Exception是指java class |
|
l**b 发帖数: 457 | 18 C#很久很久不用了,但是我记得我和我一个同学说过这个东西,我同学和我说的是C#里
面的exception都是相当于java里面的throwable,不一定要catch的,而且method里面
也不会声明有那个exception会被thrown
都赖,现在真的老老实实写try catch的很少,一般都直接在method里面继续throws
try |
|
|
c********t 发帖数: 5706 | 20 嗯,是的。实在不能throws的时候,我用catch (Exception e)代替catch各种type,然
后基本上啥都不做,或者只是 printStackTrace。我绝对懒人一个。
Throwable没用过。 :( |
|
p*****2 发帖数: 21240 | 21 刚才看了一下书,还没明白。到底什么时候用throwable呢? |
|
l*****a 发帖数: 14598 | 22 throwable包括两种
一种是Error另一种是Exception
你说的checked exception/unchecked exception |
|
|
y*******g 发帖数: 6599 | 24 Exception的解释不太对,你说的是checked和unchecked exception, unchecked
exception是runtimeexeption和subclass
不是throwable. |
|
y*******g 发帖数: 6599 | 25 著名的outofmemoryerror就是throwable啊..我也听不明白为什么要有这个。 |
|
p*****2 发帖数: 21240 | 26
outofmemoryerror是Error吗?就是lolhaha讲的那个。Throwable 有两个子类,Error和
Exception |
|
y*******g 发帖数: 6599 | 27 是啊,我唯一能想到throwable的作用就是这个。。在catch(Exception)的时候照样用
oom来crash app |
|
p*****2 发帖数: 21240 | 28
所以应该catch(Throwable)? 这样error, exception全catch住了。
那一般写production code是不是主要是catch exception呢? |
|
p*****2 发帖数: 21240 | 29
我今天也看了一下这个。
不过刚才yangcheng的意思catch(Throwable)就可以catch Error? |
|
l**b 发帖数: 457 | 30 try {
int i = 1 / 0;
} catch (Throwable t) {
System.out.println("Major error, dude!", t);
System.exit(-1);
} |
|
c******n 发帖数: 16666 | 31 我咋感觉正相反 人人都搞throwable
随时看到个好的回复 一看人账号已经删了 尤其是你去点某些/r下面top这种 回复里一
堆一堆deleted |
|
g****y 发帖数: 15 | 32 1.
catch (MyLibException ex)
{
throw ex;
}
是没意义的,catch不做任何处理有抛出还不如就把这几行去掉。
2. 那个exception handler只在当exception处理很简单时才有用,这样的design是有些
问题的,因为很多时候,一个method declare时希望明确指定具体的exception类,抛出
MyLibException和抛出Exception或Throwable并没有本质区别,都是generic exception
。 |
|
g****y 发帖数: 15 | 33 呵呵,那天没仔细看。
把没处理的exception都wrap成MyLibException,好像有问题,使用这个method的代码不
知道当MyLibException发生时,究竟是什么具体的exception。当然你在外面的代码里面
可以用myLibExceptin.getOriginalException()得到具体是什么exception,但编译器并
不会强制用户一定要处理这个exception,所以很有可能一些exception本来应该被处理的
,但用户忘了处理。这也是为什么declare method时强调要throw具体的exception class
,而不是throw一个throwable或exception。
exception handler的那个用法是可以的,是dependency injection的一个应用。但它同
样有上面说的那个问题。
用AOP的方法处理exception,也有同样的问题,看到的那些讨论认为这是一个缺陷。 |
|
g*s 发帖数: 2277 | 34 initCause(Throwable cause)
exception |
|
S********f 发帖数: 36 | 35 新手上路,大家帮忙看一下如何解决:
在用命令行(java -jar xgridagent.jar ...)安装时出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: while resolving
class:
org.beepcore.beep.core.SessionImpl
at java.lang.VMClassLoader.transformException(java.lang.Class, java.lang.
Throwable) (/usr/lib/
libgcj.so.6.0.0)
.............
Caused by: java.lang.ClassNotFoundException: sun.misc.BASE64Decoder not
found in
gnu.gcj.runtime.SystemClassLoader{urls=[file:xgridagent.jar,file:./],
parent=gnu.gcj.runtime.ExtensionClas |
|
c*****t 发帖数: 1879 | 36 The interpreter stack is different from the native method stack.
The interpreter stack (1) can be allocated on heaps, may be expandable,
and need not to be contiguous.
The native method stack (2) is a C calling stack, not expandable.
So using JNI would only provide you C-calling stack information.
However, maybe you could inspect the VM to generate the necessary
information for stack. Throwable.fillStackTrace() (spelling?) is
a native function call. If you have the source code of that piece
of |
|
f****n 发帖数: 208 | 37 Based on my experience, these are the popular ones:
1) Final, finally and finanize key words understanding
2) Synchronized keyword and wait, notify (notifyAll) situation.
3) Serialization understanding, Static varible serialization?
4) Method overiding and overloading and shadowing
5) Java 5 features, Autoboxing, Generics...
6) Exception handling, Excepetion classes structure, checked and non checked
exception, throwable and error class
7) Collections, Differenece between arraylist, vector and l |
|
f****n 发帖数: 208 | 38 Based on my experience, these are the popular ones:
1) Final, finally and finanize key words understanding
2) Synchronized keyword and wait, notify (notifyAll) situation.
3) Serialization understanding, Static varible serialization?
4) Method overiding and overloading and shadowing
5) Java 5 features, Autoboxing, Generics...
6) Exception handling, Excepetion classes structure, checked and non checked
exception, throwable and error class
7) Collections, Differenece between arraylist, vector and l |
|
f****n 发帖数: 208 | 39 Based on my experience, these are the popular ones:
1) Final, finally and finanize key words understanding
2) Synchronized keyword and wait, notify (notifyAll) situation.
3) Serialization understanding, Static varible serialization?
4) Method overiding and overloading and shadowing
5) Java 5 features, Autoboxing, Generics...
6) Exception handling, Excepetion classes structure, checked and non checked
exception, throwable and error class
7) Collections, Differenece between arraylist, vector and l |
|
f****n 发帖数: 208 | 40 Based on my experience, these are the popular ones:
1) Final, finally and finanize key words understanding
2) Synchronized keyword and wait, notify (notifyAll) situation.
3) Serialization understanding, Static varible serialization?
4) Method overiding and overloading and shadowing
5) Java 5 features, Autoboxing, Generics...
6) Exception handling, Excepetion classes structure, checked and non checked
exception, throwable and error class
7) Collections, Differenece between arraylist, vector and l |
|
f****n 发帖数: 208 | 41 Based on my experience, these are the popular ones:
1) Final, finally and finanize key words understanding
2) Synchronized keyword and wait, notify (notifyAll) situation.
3) Serialization understanding, Static varible serialization?
4) Method overiding and overloading and shadowing
5) Java 5 features, Autoboxing, Generics...
6) Exception handling, Excepetion classes structure, checked and non checked
exception, throwable and error class
7) Collections, Differenece between arraylist, vector and l |
|
j****y 发帖数: 178 | 42 用的是tomcat。
web。xml如下:
500
/error
java.lang.Throwable
/error
ExceptionHandler
ExceptionHandler
ExceptionHandler
|
|
l**s 发帖数: 28 | 43 agree.
Runnable的cast好象也没必要
return t;应该放进try/catch block里,或finally里
面试的人大概也希望你把ClassNotFoundException先catch住,然后再catch Throwable
本人新手,只知道和做过一些基本的Java,也准备找工作了,希望大家多分享一些面试
题和经验。谢谢! |
|
s*****i 发帖数: 355 | 44 不要runnable cast怎么能编译
Throwable |
|
s******e 发帖数: 493 | 45 if you ever grab a scarce resource in your construtor (I am not saying you
shoud do that, but there might be some scenario you want to do that. )
catching throwable to release it does make sense. |
|
x****a 发帖数: 1229 | 46 作业,创建rectangle class, 要求 throw exceptions, 我的code如下:
public class rectangle extends Shape{
public Rectangle ()
{
this(Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE);
}
public Rectangle (double newLength, double newWidth)
{
this(Rectangle.RECTANGLE_NAME, newLength, newWidth);
}
protected Rectangle (String newName, double newLength, double newWidth)
{
super (newName);
try
{
this.setLength (newLength);
... 阅读全帖 |
|
m****r 发帖数: 6639 | 47 你的exception需要implemnt throwable
或者直接extend 一个其他的 exception |
|
l**b 发帖数: 457 | 48 Exception本身就是implements了Throwable了的,你extends和implements达到的效果
都是一样的。
我试了你的code,没有你说的问题啊?
Shape.java:
public class Shape {
public static final double DEFAULT_SIZE = 1.0;
private String name;
public Shape(String name) {
this.name = name;
}
}
Rectangle.java:
public class Rectangle extends Shape {
public static final String RECTANGLE_NAME = "Rectangle";
private double length;
private double width;
public Rectangle ()
{
this(S... 阅读全帖 |
|
r*****l 发帖数: 2859 | 49 1, 下面的code运行结果会怎么样? Assume you don't have java in handy to test
run it.
2, 如果面试被问到,怎么回答呢?
try {
char[] cs = {'a', 'b'};
String s = null;
s = s.copyValueOf(cs);
System.out.print(s);
} catch (NullPointerException e) {
System.exit(1);
} catch (Throwable e) {
System.exit(2);
}
Note: "copyValueOf" is a static method of String class.
1, prints out "ab"
2, exit with "1"
3, exit with "2" |
|
g*****g 发帖数: 34805 | 50 StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsStrting = sw.toString();
or org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)
196) |
|