Java学习笔记之异常类

Exception的几个方法:
在这里插入图片描述

package ppp;
import java.lang.Exception;
public class start {
	public static void main(String args[]) {
		try {
			throw new Exception("my exception");
		}catch(Exception e) {
			System.out.println("e.getMessage():"+e.getMessage());
			System.err.println("e.toString():"+e);
			System.err.println("e.printStackTrace():");
			e.printStackTrace();
		}
	}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package dsm;
import java.io.*;
import java.util.Scanner;

/** Lab 13. CS1110 Exception handling */
class Lab13 {
    /** = a buffered reader attached to the keyboard. If you store this value
      *   in a variable kbd (say), then
      * 
      *      kbd.readLine()
      * 
      *   will prompt the reader to type a line by putting in the Interactions pane
      *   a field into which to type and will then yield the string of characters
      *   the user typed in.
      */    
    public static BufferedReader getKeyboard()  {
        // Create a link to the keyboard
        return new BufferedReader(new InputStreamReader(System.in));
    }
    
    
    
    /** Prompt the reader to type a line into the interactions pane and
      * return the line that the user types. */
    public static String readKeyboardLine() throws IOException {
        BufferedReader kyboard= getKeyboard();
        String line= kyboard.readLine();
        return line;
    }
    
    /** Prompt the reader to type an integer (an int) into the interactions pane
      * and return the integer that they type. If the user types something that
      * is not an int, then issue a message (System.out.println(...) and prompts
      * again.
      */
    public static int readKeyboardInt() throws IOException {
        BufferedReader kyboard= getKeyboard();
        String stream=kyboard.readLine();
        try {
        	Integer.parseInt(stream);
        }catch(Exception e) {
        	throw new IOException("输入格式错误,请您输入*整数*");
        }
        return 0;
    }
    
    /** = b**c.
          Precondition: c ?0
        */
    public static double exp(double b, int c) throws IOException {
    	if(c<0) {
    		IOException e1=new IOException("输入错误:c<0");
    		throw e1;
    	}
        if (c == 0)
            return 1;
        // c > 0
        if (c%2 == 0)
            return exp(b*b, c / 2);
        // c is odd and > 0
        return b * exp(b, c-1);
    }
    
    /** = the value i such that x**i <= .00000001 but x**(i-1) is not.
          Throw MyException if x <= 0 or 1 <= x. 
     * @throws MyException 
     * @throws IOException 
       */
    public static int approach(double x) throws MyException, IOException {
        int i= 1; // stub; you have to complete this function
        if(x<=0) {throw new MyException("MyException: x<=0");}
        else if(x>=1) {throw new MyException("MyException: x>=1");}
        else {
        	while(true) {
				if(exp(x, i)<0.1)break;
        		i++;
        	}
            return i;	
        }
    }
}
class MyException extends Exception{
	public MyException() {
		
	}
	public MyException(String str) {
		super(str);
	}
}
public class Testdzk {
	public static void main(String args[]) throws MyException, IOException {
		Lab13 test=new Lab13();
		System.out.println("***Case 1:判断输入的字符是否为数字***");
        while(true){
		  try {
			  System.out.println("请输入一个整数:");
			  test.readKeyboardInt();
			  break;
		  } catch (IOException e) {
			  System.out.println(e.getMessage());
		  }
        }
        System.out.println("恭喜您,输入成功!");
        
        System.out.println("\n***Case 2:判断c是否小于0***");
        Scanner sc=new Scanner(System.in);
        while(true){
  		  try {
  			  System.out.println("请输入c:");
  			  int c=sc.nextInt();
  			  test.exp(1, c);
  			  break;
  		  } catch (IOException e1) {
  			  System.out.println(e1.getMessage());
  		  }
         }
        System.out.println("恭喜您,输入成功!");
        
        System.out.println("\n***Case 3:测试异常类+测试approach***");
        while(true){
    		  try {
    			  System.out.println("请输入x:");
    			  double x=sc.nextFloat();
    			  System.out.println(test.approach(x));
    			  System.out.println("恭喜您,计算成功!");
    			  break;
    		  } catch (MyException e2) {
    			  System.out.println(e2.getMessage());
			}
           }
	}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅破苍穹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值