javashoot的简单介绍
本篇文章给大家谈谈javashoot,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java抛出异常在哪里处理
- 2、java题目 ,那位高手帮忙做一下,谢谢了
- 3、JAVA的throw和throws怎么用!
- 4、JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意
java抛出异常在哪里处理
如果不抛出异常则是在catch块里,抛出则由调用方法的类处理。
为了避免调用的人不知道有异常,才抛出异常的,所以是谁掉用的久在哪里处理。说的对吗
对.
1、throws关键字通常被应用在声明方法时,用来指定可能抛出的异常。多个异常可以使用逗号隔开。当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象。如下面例子所示:
public class Shoot { 创建类
static void pop() throws NegativeArraySizeException {
//定义方法并抛出NegativeArraySizeException异常
int [] arr = new int[-3];//创建数组
}
public static void main(String[] args) {//主方法
try {
pop(); //调用pop()方法
} catch (NegativeArraySizeException e) {
System.out.println("pop()方法抛出的异常");//输出异常信息
}
}
}
2、throw关键字通常用在方法体中,并且抛出一个异常对象。程序在执行到throw语句时立即停止,它后面的语句都不执行。通过throw抛出异常后,如果想在上一级代码中来捕获并处理异常,则需要在抛出异常的方法中使用throws关键字在方法声明中指明要跑出的异常;如果要捕捉throw抛出的异常,则必须使用try—catch语句。举例如下:
class MyException extends Exception { //创建自定义异常类
String message; //定义String类型变量
public MyException(String ErrorMessagr) { //父类方法
message = ErrorMessagr;
}
public String getMessage(){ //覆盖getMessage()方法
return message;
}
}
public class Captor { //创建类
static int quotient(int x,int y) throws MyException{//定义方法抛出异常
if(y 0){ //判断参数是否小于0
throw new MyException("除数不能是负数");//异常信息
}
return x/y;//返回值
}
public static void main(String args[]){ //主方法
try{ //try语句包含可能发生异常的语句
int result = quotient(3,-1);//调用方法quotient()
}catch (MyException e) { //处理自定义异常
System.out.println(e.getMessage()); //输出异常信息
}
catch (ArithmeticException e) {
//处理ArithmeticException异常
System.out.println("除数不能为0");//输出提示信息
}
catch (Exception e) { //处理其他异常
System.out.println("程序发生了其他的异常");
//输出提示信息
}
}
}
java题目 ,那位高手帮忙做一下,谢谢了
public class Dog {
private String name;
private String color;
private int weight;
private int age;
public Dog() {
}
public Dog(String name, String color, int weight, int age) {
this.name = name;
this.color = color;
this.weight = weight;
this.age = age;
}
public String toString() {
String returnStr = "Dog name:" + this.name + " Dog color:" + this.color
+ " Dog weight:" + this.weight + " Dog age" + this.age;
return returnStr;
}
public void shoot() {
System.out.println("This is " + this.name +" shoot!");
}
public void bite() {
System.out.println("This is " + this.name + " bite");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
Dog dog1 = new Dog("Dog1", "white", 5, 4);
Dog dog2 = new Dog("Dog2", "red", 6 , 4);
dog1.shoot();
dog2.bite();
}
}
已经试验过了
JAVA的throw和throws怎么用!
throw是语句抛出一个异常;语法:throw(异常对象);
throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常) ;语法:[(修饰符)](返回值类型)(方法名)([参数列表])[throws(异常类)]{......}
当某个方法可能会抛出某种异常时用于throws 声明可能抛出的异常,然后交给上层调用它的方法程序处理。如:
扩展资料:
throw与throws的区别
1、throw用在方法体内,上面代码显示了,是直接在main方法体内
throws用在方法声明后面,表示再抛出异常,由该方法的调用者来处理。这个看上面的代码就理解了。
2、throw是具体向外抛异常的,抛出的是一个异常实例。
throws声明了是哪种类型的异常,使它的调用者可以捕获这个异常。
3、throw,如果执行了,那么一定是抛出了某种异常了,安生throws表示可能出现,但不一定。
4、同时出现的时候,throws出现在函数头、throw出现在函数体,两种不会由函数去处理,真正的处理由函数的上层调用处理。
参考资料:百度百科 异常处理
JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意
throw 仅用于方法定义后面,指示该方法可能会抛出什么异常,使用该方法的方法必须处理该异常,或者再次抛出。
throws 用于当程序判断发生异常时,用该语句抛出异常,或处理异常时再次抛出异常。
//下面三个关键字就是处理异常
try {
//这里放可能会发生异常的语句
} catch(Exception e) {
//这里处理异常
} finally {
//这里的语句必然会得到执行,不管异常发省与否,
//用于关闭数据库,关闭连接等收尾操作(非必要)
}
java异常的一个简单例子,比如我有一个除法方法
public int divide(int a, int b) {
return a / b;
}
但是这就有一个问题,当b是0时,程序会报错。
如果引入异常,改成这样
public int divide(int a, int b) throws Exception {
if (b == 0) {
throw new Exception("b = 0");
}
return a / b;
}
那么调用端该怎么用呢
public class ExceptionTest {
public static void main(String[] args) {
ExceptionTest et = new ExceptionTest();
try {
System.out.println(et.divide(12, 0));
} catch (Exception e) {
System.out.println("0不能做被除数");
}
}
public int divide(int a, int b) throws Exception {
if (b == 0) {
throw new Exception("b = 0");
}
return a / b;
}
}
程序可以继续执行,不会中断。
javashoot的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javashoot的信息别忘了在本站进行查找喔。
发布于:2022-12-19,除非注明,否则均为
原创文章,转载请注明出处。