public boolean A()throws Exception
{
try
{
if(..)
{
return true;
}
else
return false;
}
catch(Exception e)
{
throw(e);
//return true; (用写这句么?)
}
}
我想问一下,如果我在catch里面抛出异常,然后是否还要写return true;
大家帮我看看,谢谢大家
{
try
{
if(..)
{
return true;
}
else
return false;
}
catch(Exception e)
{
throw(e);
//return true; (用写这句么?)
}
}
我想问一下,如果我在catch里面抛出异常,然后是否还要写return true;
大家帮我看看,谢谢大家
解决方案 »
- 处理用户异常退出
- java如何打开文本文件
- 关于hashCode()问题请教
- 请问,将某个目录下的文件复制到一个新的目录,怎么实现,谢谢
- 已知道TreePath,但是用tree.expandPath(path)为什么打不开呀
- 网上哪儿有在win98上的jedk1.3可以下载,sun的是1.31?
- 请问如何判断一个站点是否支持断点续传!
- 请问jbuiler6企业版的注册是怎么回事?
- 有没有关于JDBC 2.0(3.0) 分页技术的文章
- 请问有没有人熟悉Java调用本地程序?
- GridBagLayout布局管理器中的权值(weight value)是个什么意思?
- 关于ResourceBundle读取properties文件的一个问题
先定义一个 ret变量
boolean ret = fasle中间改变这个 ret的值最后返回这个 ret 变量。
如果出错时,需要返回的值,则不抛出异常。
{
try
{
if(..)
{
return true;
}
else
return false;
}
catch(Exception e)
{
// throw(e);
System.out.println(e);
return true;
}
这样不就行了.
}
public boolean A()throws Exception
{
try
{
if(..)
{
return true;
}
else
return false;
}
catch(Exception e)
{
// throw(e);
System.out.println(e);
return true;
}
//这样不就行了.
}应该是这样才行
呵呵.
正确的做法:
多个catch 处理能处理的,throw不能处理的try{
}catch(ExceptionOne e1){
....
retrun false;
}catch(ExceptionTwo e2){
....
retrun false;
}catch(Exception e3){
....
throw (e3);
}
你完全可以finally语句块中return boolean
try
{
if( ...)
{
...;
}
if( ...)
{
...;
}
}
catch(Exception e )
{}
finally
{
return true;
}
throws Exception是方法声明, 和下面的throw new Exception()不是一回事!!
一个是throws, 一个是throw.
{
boolean bRet = true; try
{
if(...) //true
{
//...
}
else
{
bRet = false;
}
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
bRet = false;
} return bRet;
}中的throws Exception可以去掉。异常或抛出,或捕捉,既然捕捉了,就不要抛出了
try {
...
}
catch( Exception e) {
...
throw( new Exception());
//在你收到异常的时候再抛出异常?
//编写者期望在函数A外面的try块中接收这个异常。结果并非你所期望的哦!
}
测试代码:
======================================================================
class MyException extends Exception { private String errMesg;
MyException(String str) {
errMesg = str;
}
public String toString() {
return errMesg;
}
}class MyException extends Exception { private String errMesg;
MyException(String str) {
errMesg = str;
}
public String toString() {
return errMesg;
}
}public class CatchTest0 { static void checkError(int nParam) {
try {
System.out.println("checkError("+nParam+");==========================");
funA(nParam);
System.out.println("\n");
} catch(Exception e) {
System.out.println("函数抛出异常:"+e);
}
} static Boolean funA(int nParam) throws Exception { //做向上转型;
try {
if(nParam==1) {
System.out.print("内部 空");
throw new NullPointerException("内部:空指针;1");//手动产生异常
}
else if(nParam==2) {
System.out.print("内部 数");
throw new IndexOutOfBoundsException("内部:数组越界;2");
}
else if(nParam==3) {
System.out.print("内部 运");
throw new ArithmeticException("内部:运算异常;3");
}
else {
System.out.println("我没错");
return true;
} } catch(ArithmeticException e) {
System.out.println("---我接收到了 运");
try {
int[] arr = {5,4,3};
arr[5] = 1;//自动创建异常
} catch(ArrayIndexOutOfBoundsException ee) {
System.out.println("下标越界;");
} finally {
throw new MyException("外部:我的 运算异常");
} } catch(IndexOutOfBoundsException e) {
System.out.println("---我接收到了 数");
throw(e);//抛出接收到的异常 } catch(NullPointerException e) {
System.out.println("---我接收到了 空");
throw new MyException("外部:我的 空指针");//抛出自定义的异常。// 无效的捕捉。系统未产生MyException类异常!
// } catch(MyException e) {
// System.out.println("receive a exception.");
// throw new MyException("BAD INOF"); //已经获取异常,无须再抛出!
//
} finally {
return true;
//函数funA() 返回true;
}
} public static void main(String[] args) { checkError(1);
checkError(2);
checkError(3);
checkError(4);
}
}
结果为:---------------------------------------意料之外吧~~
D:\MyJava\Catch>javac CatchTest0.javaD:\MyJava\Catch>java CatchTest0
checkError(1);==========================
内部 空---我接收到了 空checkError(2);==========================
内部 数---我接收到了 数checkError(3);==========================
内部 运---我接收到了 运
下标越界;checkError(4);==========================
我没错D:\MyJava\Catch>
class MyException extends Exception { private String errMesg;
MyException(String str) {
errMesg = str;
}
public String toString() {
return errMesg;
}
}public class CatchTest1 {
static void checkError(int nParam) {
try {
System.out.println("checkError("+nParam+");==========================");
funA(nParam);
System.out.println("我没错!\n");
} catch(MyException e) {
System.out.println("函数抛出异常:"+e);
}
} static Boolean funA(int nParam) throws MyException { //做向上转型; if(nParam==3) {
System.out.println("---我接收到了 运");
throw new MyException("外部:我的 运算异常");
}
else if(nParam==2) {
System.out.println("---我接收到了 数");
throw new MyException("外部:我的 数组越界");
}
else if(nParam==1) {
System.out.println("---我接收到了 空");
throw new MyException("外部:我的 空指针");
}
else {
return true;
//函数funA() 返回true;
}
} public static void main(String[] args) { checkError(1);
checkError(2);
checkError(3);
checkError(4);
}
}
--------------------------------------------------
D:\MyJava\Catch>javac CatchTest1.javaD:\MyJava\Catch>java CatchTest1
checkError(1);==========================
---我接收到了 空
函数抛出异常:外部:我的 空指针
checkError(2);==========================
---我接收到了 数
函数抛出异常:外部:我的 数组越界
checkError(3);==========================
---我接收到了 运
函数抛出异常:外部:我的 运算异常
checkError(4);==========================
我没错!
D:\MyJava\Catch>