异常
| public static void main(String[] args) { |
| |
| try { |
| int i = 10 / 0; |
| System.out.println(i); |
| } catch(Exception e) { |
| |
| String message = e.getMessage(); |
| System.out.println("异常信息:" + message); |
| |
| e.printStackTrace(); |
| } finally { |
| System.out.println(111); |
| } |
| System.out.println(222); |
| } |
| |
| # 运行结果 |
| 异常信息:/ by zero |
| 111 |
| 222 |
| |
| static void f1() throws Exception { |
| try { |
| int i = 10/0; |
| System.out.println(i); |
| } catch (Exception e) { |
| |
| throw new Exception(); |
| } |
| } |
| |
| |
| public static void main(String[] args) throws Exception { |
| |
| f1(); |
| } |
| |
- 捕获多个异常时,因为程序是从上往下运行,所以小的异常类型必须放在前面
| static Integer f(String str) { |
| try { |
| return Integer.parseInt(str); |
| } catch (ArrayIndexOutOfBoundsException e) { |
| System.out.println("捕获到异常"); |
| } catch(NumberFormatException e) { |
| e.printStackTrace(); |
| } catch (ClassCastException e) { |
| System.out.println("类型转换"); |
| } catch (Exception e) { |
| System.out.println("最大的异常"); |
| } |
| return 0; |
| } |
| |
| |
| public static void main(String[] args){ |
| try { |
| int f; |
| f = f("111a"); |
| System.out.println(f); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| public class MyException extends RuntimeException { |
| |
| private static final long serialVersionUID = 1L; |
| |
| MyException() { |
| super(); |
| } |
| |
| MyException(String msg) { |
| super(msg); |
| } |
| } |
| |
| public class Test { |
| static void f(String str) throws MyException{ |
| try { |
| int i = Integer.parseInt(str); |
| } catch (MyException e) { |
| System.out.println("111"); |
| throw new MyException("类型转换错误"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| f("a"); |
| } |
| } |
泛型
| |
| public static void main(String[] args) { |
| List<String> list = new ArrayList<String>(); |
| list.add("adc"); |
| } |
| |
| |
| public class Test { |
| |
| public static < E > void printArray( E[] inputArray ) { |
| |
| for ( E element : inputArray ){ |
| System.out.printf( "%s ", element ); |
| } |
| System.out.println(); |
| } |
| } |
| |
| # 格式:访问修饰符 [static] <T> void 方法名(T t){方法体} |
| public void f(){ # 普通方法 |
| } |
| public T f() { # 类泛型化后的方法 |
| } |
| public void f(T t) { # 普通方法,传入的参数可以是泛型 |
| } |
| public <T> void f(T t){ # 泛型方法 |
| } |
| |
| public class Box<T> { |
| private T t; |
| |
| public void add(T t) { |
| this.t = t; |
| } |
| |
| public T get() { |
| return t; |
| } |
| |
| |
| public static void main(String[] args) { |
| Box<Integer> integerBox = new Box<Integer>(); |
| Box<String> stringBox = new Box<String>(); |
| |
| integerBox.add(new Integer(10)); |
| stringBox.add(new String("菜鸟教程")); |
| |
| System.out.printf("整型值为 :%d\n\n", integerBox.get()); |
| System.out.printf("字符串为 :%s\n", stringBox.get()); |
| } |
| } |
| |
| public interface Interface01 <M>{ |
| public abstract void f1(M m); |
| } |
| |
| public class Interface01impl<M> implements Interface01<M>{ |
| @Override |
| public void f1(M m) { |
| System.out.println(m); |
| } |
| } |
| |
| public class Interface02impl implements Interface01<String> { |
| @Override |
| public void f1(String s) { |
| System.out.println(s); |
| } |
| } |
| |
| public class Test { |
| |
| public static void getData(List<?> data) { |
| System.out.println("data :" + data.get(0)); |
| } |
| |
| |
| |
| public static void main(String[] args) { |
| List<String> name = new ArrayList<String>(); |
| List<Integer> age = new ArrayList<Integer>(); |
| List<Number> number = new ArrayList<Number>(); |
| |
| name.add("icon"); |
| age.add(18); |
| number.add(314); |
| |
| getData(name); |
| getData(age); |
| getData(number); |
| } |
| } |
| |
| |
| |
| public class Test { |
| public static void getUperNumber(List<? extends Number> data) { |
| System.out.println("data :" + data.get(0)); |
| } |
| |
| public static void main(String[] args) { |
| List<String> name = new ArrayList<String>(); |
| List<Integer> age = new ArrayList<Integer>(); |
| List<Number> number = new ArrayList<Number>(); |
| |
| name.add("icon"); |
| age.add(18); |
| number.add(314); |
| |
| |
| getUperNumber(age); |
| getUperNumber(number); |
| } |
| |
| } |
| |
| |
| public class Fruit { |
| } |
| |
| |
| public class Apple extends Fruit { |
| } |
| |
| |
| |
| |
| public class Test { |
| |
| static <T extends Apple> void f(T t) { |
| } |
| |
| public static void main(String[] args) { |
| |
| f(new Apple()); |
| f(new Orange()); |
| |
| |
| List<? extends Apple> l = new ArrayList<Apple>(); |
| |
| l.add(null); |
| System.out.println(l.get(0)); |
| |
| |
| List<? super Fruit> l2 = new ArrayList<Fruit>(); |
| l2.add(new Orange()); |
| l2.add(new Fruit()); |
| l2.add(new Apple()); |
| } |
| } |
| |
| |
| <T extends ClassName> |
| |
| |
| <? extends ClassName> |
| |
| <? super ClassName> |
| |
| <?> |
| |
| <? extends T> |
| |
| <? super T> |
| |
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术