认识异常,以及Exception和RuntimeException的区别。
一、不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
例如:abc def -->abcdef
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] value = str.split(" ");
String ret = “”;
for (int i = 0; i < value.length; i++) {
ret += value[i];
}
System.out.println(ret);
}
二、反转整个字符串。
public static String reverse(String str,int start,int end) {
char[] value = str.toCharArray();
while (start < end) {
char tmp = value[start];
value[start] = value[end];
value[end] = tmp;
start++;
end–;
}
return String.copyValueOf(value);
}
三、给一个字符类型的数组chas和一个整数size,请把大小为size的左半区整体右移到右半区,右半区整体移动到左边。
例如:abcdefg --> defgabc。请用代码实现。
//str-->abcdefg 3 ""
public static String reverseSentence(String str,int n) {
if(str == null || str.length()==0
|| n < 0 || n > str.length()){
return null;
}
int s1 = 0;
int e1 = n-1;
int s2 = n;
int e2 = str.length()-1;
str = reverse(str,s1,e1);
str = reverse(str,s2,e2);
str = reverse(str,s1,e2);
//调用reverse函数,反转指定位置的字符串
return str;
}
//打印这个字符串数组。
public static void main3(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
String string = scanner.next();
String s = reverseSentence(string,n);
System.out.println(s);
}
四、异常体系。
1.try-catch语句处理异常,try中的代码执行到异常停止,之外的代码依次会继续执行。
2.如果没有try-catch语句进行异常处理,则由JVM自行处理异常,那么程序会停止到异常处,不会执行后续的代码。
3.针对ArrayIndexOutOfBoundsException 和 NullPointerException异常的try-catch处理。
分别是下标越界异常和空指针异常。
public static void main1(String[] args) {
int[] arr = {1, 2, 3};
try{
System.out.println(“before”);
arr = null;
System.out.println(arr[100]);
System.out.println(“after”);
}catch (Exception e) {
System.out.println(“发生异常”);
e.printStackTrace();
}
/catch (ArrayIndexOutOfBoundsException
| NullPointerException e) {
System.out.println(“发生异常”);
//打印出栈当中的追踪信息
e.printStackTrace();
}catch (NullPointerException e) {
System.out.println(“空指针发生异常”);
//打印出栈当中的追踪信息
e.printStackTrace();
}/
System.out.println(“after try catch”);
}
打印结果:
before,发生异常,after try catch
充分说明,使用try-catch语句,可处理异常且不影响语句外程序的运行。
五、finally块一定会被执行。
public static int func2() {
try {
int a = 10/5;
return 10;
}catch (ArithmeticException e){
return 30;
}finally {
//建议 不要在finally当中写return
//finally当中写return 会抑制try
// 和 catch当中的return
return 20;
}
}
public static void main5(String[] args) {
System.out.println(func2());
}
打印结果:
20
①finally块中的运行结果,将之前的运行结果30覆盖掉,由此可以得知,final块语句中,尽量不要采用return语句,以免发生故障。
②finally块一般用于资源的关闭。关闭连接的数据库资源。
六、throw的作用主要为声明。
//throws->方法的后面 声明
public static int divide(int x, int y)
throws ArithmeticException {
if (y == 0) {
//throw:–>特点的异常-》自定义的异常
throw new ArithmeticException(“抛出除 0 异常”);
}
return x / y;
}
抛出一个异常,声明异常的类型。异常情况由JVM处理,程序会运行停止。
七、关于索引文件夹中文件的一段代码,包括如果找不到,如何输出此异常。
public static void main6(String[] args) {
System.out.println(readFile());
}
public static String readFile() {
// 尝试打开文件, 并读其中的一行.
File file = new File("d:/test.txt");
// 使用文件对象构造 Scanner 对象.
try {
Scanner sc = new Scanner(file);
System.out.println("fafkjkafjaljflksfja");
return sc.nextLine();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
八、用户名登录以及密码验证过程中,程序输入以及异常处理的代码。
class UserException extends Exception {
public UserException(String message) {
super(message);
//使用super关键字访问父类的成员变量、成员方法。
//使用super关键字访问父类的构造方法,必须位于第一行,且只能被执行一次。
}
}
class PasswordException extends Exception {
public PasswordException(String message) {
super(message);
}
}
public class TestDemo2 {
private static String userName = “admin”;
private static String password = “123456”;
public static void main(String[] args) {
try {
login("admin", "123456");
} catch (UserException | PasswordException userError) {
userError.printStackTrace();
}
}
public static void login(String userName, String password)
throws UserException, PasswordException {
if (!TestDemo2.userName.equals(userName)) {
throw new UserException("用户名错误");
}
if (!TestDemo2.password.equals(password)) {
throw new PasswordException("密码错误");
}
System.out.println("登陆成功");
}
九、关于异常类的注意事项:
1.顶层类 Throwable 派生出两个重要的子类, Error 和 Exception其中 Error 指的是 Java 运行时内部错误和资源耗尽错误.
2.应用程序不抛出此类异常. 这种内部错误一旦出现,
除了告知用户并使程序终止之外, 再无能无力. 这种情况很少出现.
3.Exception 是我们程序猿所使用的异常类的父类.
其中 Exception 有一个子类称为 RuntimeException , 这里面又派生出很多我们常见的异常类。NullPointerException , IndexOutOfBoundsException 等.