java学习--异常处理 1k 2026-05-12T08:34:00.000Z 2026-05-12T08:34:00.000Z 4 分钟
原文链接:https://www.cnblogs.com/Time25/p/20023821.html
异常处理的例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.io.UnsupportedEncodingException;import java.util.Arrays;public class Main { public static void main (String[] args) { byte [] bs = toGBK("中文" ); System.out.println(Arrays.toString(bs)); } static byte [] toGBK(String s) { try { return s.getBytes("GBK" ); } catch (UnsupportedEncodingException e) { System.out.println(e); return s.getBytes(); } } }
什么是异常 异常可以理解为一个类,这个类的体系结构如下,1.error无需捕获,无能为力,例如:OutOfMemoryError:内存耗尽 NoClassDefFoundError:无法加载某个Class StackOverflowError:栈溢出
2.exception是需要捕获的,Exception则是运行时的错误,它可以被捕获并处理。 某些异常是应用程序逻辑处理的一部分,应该捕获并处理。例如: NumberFormatException:数值类型的格式错误 FileNotFoundException:未找到文件 SocketException:读取网络失败
3.还有一些异常时程序的问题:例如程序编写逻辑不对,应该修复程序本身。例如: NullPointerException:对某个null的对象调用方法或字段 IndexOutOfBoundsException:数组索引越界,也正因如此,才有下面的规定
并非所有的Exception都是要捕获的: Exception又分为两大类: RuntimeException以及它的子类; 非RuntimeException(包括IOException、ReflectiveOperationException等等) Java规定: 必须捕获的异常,包括Exception及其子类,但不包括RuntimeException及其子类,这种类型的异常称为Checked Exception。 不需要捕获的异常,包括Error及其子类,RuntimeException及其子类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ┌───────────┐ │ Object │ └───────────┘ ▲ │ ┌───────────┐ │ Throwable │ └───────────┘ ▲ ┌─────────┴─────────┐ │ │ ┌───────────┐ ┌───────────┐ │ Error │ │ Exception │ └───────────┘ └───────────┘ ▲ ▲ ┌───────┘ ┌────┴──────────┐ │ │ │ ┌─────────────────┐ ┌─────────────────┐┌───────────┐ │OutOfMemoryError │... │RuntimeException ││IOException│... └─────────────────┘ └─────────────────┘└───────────┘ ▲ ┌───────────┴─────────────┐ │ │ ┌─────────────────────┐ ┌─────────────────────────┐ │NullPointerException │ │IllegalArgumentException │... └─────────────────────┘ └─────────────────────────┘
异常捕获的必要性,在程序中,有些类中的某些方法会利用throws 关键字来指出该方法可能出现的异常 例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public byte [] getBytes(String charsetName) throws UnsupportedEncodingException { ... } import java.io.UnsupportedEncodingException;import java.util.Arrays;public class Main { public static void main (String[] args) { byte [] bs = toGBK("中文" ); System.out.println(Arrays.toString(bs)); } static byte [] toGBK(String s) throws UnsupportedEncodingException { return s.getBytes("GBK" ); } } import java.io.UnsupportedEncodingException;import java.util.Arrays;public class Main { public static void main (String[] args) { try { byte [] bs = toGBK("中文" ); System.out.println(Arrays.toString(bs)); } catch (UnsupportedEncodingException e) { System.out.println(e); } } static byte [] toGBK(String s) throws UnsupportedEncodingException { return s.getBytes("GBK" ); } } import java.io.UnsupportedEncodingException;import java.util.Arrays;public class Main { public static void main (String[] args) throws Exception { byte [] bs = toGBK("中文" ); System.out.println(Arrays.toString(bs)); } static byte [] toGBK(String s) throws UnsupportedEncodingException { return s.getBytes("GBK" ); } }
对于异常处理的记录 如果发现异常但是什么都不做时非常不好的,这个时候可以利用
1 2 3 4 5 6 7 8 9 static byte [] toGBK(String s) { try { return s.getBytes("GBK" ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null ;
// TRAINING LOG · 第 63 / 76 篇训练记录 · 成文于 2026.05.12 · 下午 16:34