用try…catch异常的处理
1. 编写一个类,处理由被0整除产生的异常
2. 编写一个类,处理数组越界产生的异常
3. 自定义一个异常,名称是NotEnglishException,该异常用来处理一个是否是英文字符串,用throw抛出该异常,用throws处理该异常
1. 编写一个类,处理由被0整除产生的异常
2. 编写一个类,处理数组越界产生的异常
3. 自定义一个异常,名称是NotEnglishException,该异常用来处理一个是否是英文字符串,用throw抛出该异常,用throws处理该异常
public class TryTest {
public static void main(String[] args) {
try{
int m = 6 , n = 0 ;
int sum = m/n ;
}catch(Exception e){
e.printStackTrace();
}
}
}
package com.train.first;import java.util.regex.Pattern;public class Test
{
public static void main(String[] args)
{
arithmetic();
outOfbounds();
try
{
notEnglish();
}
catch (NotEnglishException e)
{
e.printStackTrace();
}
}
private static void arithmetic()
{
try
{
int i = 10, j = 0;
System.out.println(i / j);
}
catch (ArithmeticException e)
{
e.printStackTrace();
}
}
private static void outOfbounds()
{
try
{
int[] arr = new int[1];
System.out.println(arr[1]);
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
private static void notEnglish() throws NotEnglishException
{
if (!Pattern.matches("^[a-zA-Z]+$", "abcA123"))
{
throw new NotEnglishException("Not english");
}
}
}class NotEnglishException extends Exception
{
private static final long serialVersionUID = 1L; public NotEnglishException()
{
super();
}
public NotEnglishException(String message)
{
super(message);
}
public NotEnglishException(Exception e)
{
super(e.getMessage(), e.getCause());
}
}
NotEnglishException()
{
}
NotEnglishException(String msg)
{
super(msg);
}}
public class IsEnglishCh {
public static void main(String[] args) {
try{
String str="钱振涛说:I am a jj";
throw new NotEnglishException("沈哥说:非常正确!");
}
catch(NotEnglishException e){
e.printStackTrace();
}
}
}
import java.io.*;class NotEnglishException extends Exception
{
public NotEnglishException(String s)
{
super(s);
}
}
class Input
{
public void input() throws NotEnglishException //循环中的异常未被捕获,继续抛出,等待调用者捕获
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串:");
try
{
while(br.read()!=-1)
{
String s=br.readLine();
for(int i=0;i<s.length();i++)
{
if(!((s.charAt(i)>='a' && s.charAt(i)<='z')||(s.charAt(i)>='A' && s.charAt(i)<='Z')))
{
throw new NotEnglishException("字符串中"+s.charAt(i)+"不是英文字符,请重新输入字符串:");
}
}
}
}
catch(IOException e) //捕获readLine()方法产生的IO异常
{
System.out.println(e.getMessage());
}
}
}
public class ExceptionTest
{
public static void main(String[] args)
{
try
{
Input in=new Input();
in.input();
}
catch(NotEnglishException e) //捕获input()方法抛出的异常
{
System.out.println(e.getMessage());
}
}}
import java.io.*;class NotEnglishException extends Exception
{
public NotEnglishException(String s)
{
super(s);
}
}
class Input
{
public void input() throws NotEnglishException //循环中的异常未被捕获,继续抛出,等待调用者捕获
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串:");
try
{
while(br.read()!=-1)
{
String s=br.readLine();
for(int i=0;i<s.length();i++)
{
if(!((s.charAt(i)>='a' && s.charAt(i)<='z')||(s.charAt(i)>='A' && s.charAt(i)<='Z')))
{
throw new NotEnglishException("字符串中"+s.charAt(i)+"不是英文字符,请重新输入字符串:");
}
}
}
}
catch(IOException e) //捕获readLine()方法产生的IO异常
{
System.out.println(e.getMessage());
}
}
}
public class ExceptionTest
{
public static void main(String[] args)
{
try
{
Input in=new Input();
in.input();
}
catch(NotEnglishException e) //捕获input()方法抛出的异常
{
System.out.println(e.getMessage());
}
}
}
import java.io.*;class NotEnglishException extends Exception
{
public NotEnglishException(String s)
{
super(s);
}
}
class Input
{
public void input() throws NotEnglishException //循环中的异常未被捕获,继续抛出,等待调用者捕获
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串:");
try
{
while(br.read()!=-1)
{
String s=br.readLine();
for(int i=0;i<s.length();i++)
{
if(!((s.charAt(i)>='a' && s.charAt(i)<='z')||(s.charAt(i)>='A' && s.charAt(i)<='Z')))
{
throw new NotEnglishException("字符串中"+s.charAt(i)+"不是英文字符,请重新输入字符串:");
}
}
}
}
catch(IOException e) //捕获readLine()方法产生的IO异常
{
System.out.println(e.getMessage());
}
}
}
public class ExceptionTest
{
try
{
Input in=new Input();
in.input();
}
catch(NotEnglishException e) //捕获input()方法抛出的异常
{
System.out.println(e.getMessage());
}
}
}