RCP项目中生成自己的Problem Occurred出错对话框(通过throw Unchecked Exception)

在Eclipse中经常会由于某些异常,跳出“Problem Occurred”的出错对话框,如下所示:

这个是由于系统catch到不受检查异常(uncheckedexception),由系统自动生成。

所谓“不受检查异常(uncheckedexception)”就是说,当程序中可能出现这类异常时,即使没有用try...catch语句捕获它,也没有用throws字句声明抛出它,还是会编译通过。例如,当除数为零时,就会抛出java.lang.ArithmeticException异常。
这样的异常主要是运行时异常(RuntimeException类及其子类),常见异常包括如下:

常见异常:
ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, ClassCastException, EmptyStackException, IllegalArgumentException, IndexOutOfBoundsException(ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException), JMRuntimeException, NegativeArraySizeException, NoSuchElementException, NullPointerException, SystemException,

我们可以根据自己项目的需要在需要给用户提示出错信息时,抛出throw一个这样的异常:

throw new IllegalArgumentException("Incorrect syntax in XXXXXXXXXXX");

 这样不用try...catch,也不用throws声明抛出,我们就可以让系统自动为我们生成像上面的那个Problem Occurred出错对话框,来提示用户相关出错信息。

另外,当然你也可以用SWT的MessageDialog或者MessageBox之类的来生成一个出错窗口。

猜你喜欢

转载自guhanjie.iteye.com/blog/1697820