当前位置:首页>开发>正文

java 手动抛异常 JAVA如何抛出异常

2023-04-10 15:00:45 互联网 未知 开发

java 手动抛异常 JAVA如何抛出异常

JAVA如何抛出异常

从他处拷贝的,也许对你有用。其实,Java书上都应该有这一章节,认真看应该不会太难入门。
如果你知道你写的某个函数有可能抛出异常,而你又不想在这个函数中对异常进行处理,只是想把它抛出去让调用这个函数的上级调用函数进行处理,那么有两种方式可供选择:

第一种方式:直接在函数头中throws SomeException,函数体中不需要try/catch。比如将最开始的例子中的testEx2改为下面的方式,那么testEx1就能捕捉到testEx2抛出的异常了。
boolean testEx2() throws Exception{
boolean ret = true
int b=12
int c
for (int i=2i>=-2i--){
c=b/i
System.out.println("i=" i)
}
return true
}
第二种方式:使用try/catch,在catch中进行一定的处理之后(如果有必要的话)抛出某种异常。例如上面的testEx2改为下面的方式,testEx1也能捕获到它抛出的异常:
boolean testEx2() throws Exception{
boolean ret = true
try{
int b=12
int c
for (int i=2i>=-2i--){
c=b/i
System.out.println("i=" i)
}
return true
}catch (Exception e){
System.out.println("testEx2, catch exception")
Throw e
}
}
第三种方法:使用try/catch/finally,在catch中进行一定的处理之后(如果有必要的话)抛出某种异常。例如上面的testEx2改为下面的方式,testEx1也能捕获到它抛出的异常:
boolean testEx2() throws Exception{
boolean ret = true
try{
int b=12
int c
for (int i=2i>=-2i--){
c=b/i
System.out.println("i=" i)
throw new Exception("aaa")
}
return true
}catch (java.lang.ArithmeticException e){
System.out.println("testEx2, catch exception")
ret = false
throw new Exception("aaa")
}finally{
System.out.println("testEx2, finally return value=" ret)
}

}

java何时如何抛出异常

java 如果有异常出现时,会强制你加上 try catch 语句块,你也可以手动给自己想要做异常处理的语句块加上try{} catch{} 还可以自己抛出异常 new Exception()

java手动throws异常和系统自动抛异常有什么区别?

不是太明白什么叫手动throw exception。如果你是说自己的代码里有throw这样的语句的话,那我可以告诉你,任何exception都是这样的代码产生的。你自己的代码中加入throw语句的灵活性在于
1. 你可以定义自己的exception class,而不一定要用Java库中提供的
2. 你可以选择throw的时机,比如你catch了一个exception,想处理一下,但又想让上一层继续处理,就可以在catch block里先处理,再throw。

在JAVA中如何向上抛异常


DAO类方法中可以用两种方式抛异常
1.人为控制
如查询结果不符合要求自己 throw 一个 Exception
if(returnCount==0) throw new Exception("查询结果为空")
不推荐此种用法
2.直接在方法名后面跟上 throws Exception{
如 public List getAllParentMenus(String menuId)throws MyException{...},这样,如果此方法中运行时出现异常,那么就会把异常往上抛
抛出去的异常可以在 Usermanager 类中的方法里抓住...
比如,如果 Usermanager中的方法 methodA中调用了 DAO类中的抛异常的方法 methodB
则可以在 methodA中写成这样
try{
new DAO().methodB()
}catch(Exception e){
String 输出=e.getMessage()
}

在这输出信息可以根据抛出的异常类型,或者自定义异常中的关键字段,随机定义内容....

这个java程序为什么会抛出异常?

public static void main(String[] args) {
int count=5
int sum=0
String str=new String()
for(int i=0i<5i ){
str=str "" 2
}
System.out.println(str)
for(int i=0i<=5i ){
int k=0
String temp = str.substring(0, i)
if(!("".equals(temp))){
k=Integer.parseInt(temp)
}
System.out.println(k)
sum=sum k
}
System.out.println(sum)
}
拿我这个去跑试试,你先判断下str.substring(0, i)是否为“”了再用Integer.parseInt(temp)就不会出错了,这就是你没做异常处理啊!

JAVA关于抛出异常的题

import java.text.SimpleDateFormat
import java.util.*
public class InsuranceCalculater {
private final int mbYear=1900
private final int age_=16
private void isvaild(int age) throws IsVaildAgeException{
if(agethrow new IsVaildAgeException() } } private int getAge(int birthYear){ int tempage=0 SimpleDateFormat dateformat = new SimpleDateFormat("yyyy") int year=Integer.parseInt(dateformat.format(new Date())) if(birthYear>mbYear){ tempage=year-birthYear } return tempage>0?tempage:0 } public int baoxian(int birthYear){ int age=getAge(birthYear) int baoxian=0 try{ isvaild(age) if((age-age_)<4){ baoxian=1000 }else{ baoxian=600 } }catch(IsVaildAgeException e){ e.printStackTrace() }finally{ return baoxian } } public static void main(String[] args){ System.out.println(new InsuranceCalculater().baoxian(1984)) System.out.println(new InsuranceCalculater().baoxian(1990)) System.out.println(new InsuranceCalculater().baoxian(1900)) System.out.println(new InsuranceCalculater().baoxian(1995)) System.out.println(new InsuranceCalculater().baoxian(1993)) System.out.println(new InsuranceCalculater().baoxian(1970)) System.out.println(new InsuranceCalculater().baoxian(1821)) System.out.println(new InsuranceCalculater().baoxian(2008)) System.out.println(new InsuranceCalculater().baoxian(2010)) } } class IsVaildAgeException extends Exception{ public IsVaildAgeException(){ super("年龄需大于16岁!") } }

最新文章