exception

Error 和 Exception的区别

不打扰是莪最后的温柔 提交于 2020-03-02 15:15:13
错误和异常的区别(Error vs Exception) 今天面试问了这样一个问题,"Error" 和 "Exception"之间有啥区别?我觉得挺有意思,但是似乎又不能脱口而出。查找了一番资料之后,稍微总结了一下。 1) error都是继承自父类java.lang.Error,而exception都继承自java.lang.Exception. 2) 再看看JDK中对于java.lang.Error和java.lang.Exception的解释。 java.lang.Error : An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. 即:Error是Throwable的子类,用于标记严重错误。 合理的应用程序不应该去try/catch这种错误。绝大多数的错误都是非正常的,就根本不该出现的。 java.lang.Exception : The class Exception and its subclasses are a form of Throwable that indicates conditions

Java异常处理机制与最佳实践

前提是你 提交于 2020-03-02 09:01:54
  这周小组内的学习是探讨Java异常处理的最佳实践,今天周末,外面太闷,宅在家里对Java的异常处理做一个总结,如有不对的地方欢迎指正~ 一. 谈谈个人对Java异常处理的看法   维基百科对于异常处理的定义是: 异常处理,是编程语言或计算机硬件里的一种机制,用于处理软件或信息系统中出现的异常状况(即超出程序正常执行流程的某些特殊条件)。   Java语言从设计之初就提供了对异常处理的支持,并且不同于其它语言,Java对于异常采取了强校验机制,即对于编译期异常需要API调用方显式地对异常进行处理,这种强校验机制被一部分人所钟爱,也有一部分人狂吐槽它。持支持观点的人认为这种机制可以极大的提升系统的稳定性,当存在潜在异常的时候强制开发人员去处理异常,而反对的人则认为强制异常处理降低了代码的可读性,一串串的 try-catch 让代码看起来不够简洁,并且不正确的使用不仅达不到提升系统稳定性的目的,反而成为了bug的良好栖息之地。   Java的异常处理是一把双刃剑,这是我一向持有的观点。个人认为我们不能对Java的异常处理片面的下一个好或者不好的定义,黑格尔说“存在即合理”,既然Java的设计者强制要求我们去处理Java的异常,那么与其在那里吐槽,还不如去学习如何用好Java的异常处理,让其为提升程序的稳定性所服务。不过从笔者的亲身感受来看,用好Java的异常处理门槛并不低! 二.

Java checkedException and uncheckedException

天涯浪子 提交于 2020-03-02 08:28:00
Java主要分为两种异常: checked Exception.可以简单理解为必须捕获的异常,强制抛出。 unchecked Exception.也就是运行时异常,不用显式抛出,运行时有可能被抛出,client code 无法处理该类yichan。包括 ArithmeticException、 ClassCastException、 IndexOutOfBoundsExceptio n、 ArrayIndexOutOfBoundsExc eption, StringIndexOutOfBoundsEx ception、 IllegalArgumentException、 NumberFormatException、 NullPointerException Error、RuntimeException及其子类是unchecked Exception,其它的归为checked Exception。 Error是java自己的错误或者诸如内存耗尽等严重错误,是不可抗拒的,client无法处理,显然也没有必要去捕捉。 RuntimeException是你的程序有逻辑错误,是程序员自己的编码问题,尽量避免就是了。比如NullPointerException等。当遇到这种错误时,java将这个错误自动捕捉到,显示到concole里,然后继续运行。而checked

注解+反射+JDBC,实现一个简易的泛型DAO接口

↘锁芯ラ 提交于 2020-03-01 13:13:06
一、实现思路 1、定义3个Annotation(注解):Entity、Id、Column,Entity作用于Type级别,用于标识JavaBean与数据库表名的映射关系。Id作用于Field级别,用于标识JavaBean中ID属性与表中ID字段的映射关系,Column作用于Field级别,用于标识JavaBean中除ID属性外的其它属性与表中字段的映射关系。 2、在Dao实现类中,通过反射API获得JavaBean中注解和属性的信息,如:表名、字段。JavaBean属性的名称、数据类型等信息。然后将这些信息拼接成一条SQL语句,通过JDBC的方式与数据库交互。 二、示例代码 1、定义一个Dao公共类,提供获得数据库连接与释放数据库资源的接口 package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 提供获取数据库连接、释放资源的接口 */ public class JdbcDaoHelper { /** * 数据库用户名 */ private static final String USER =

exception specification of overriding function is more lax than base version

三世轮回 提交于 2020-02-23 09:14:40
问题 I want to custom an Exception class, here's the code: class TestException : std::exception{ public: const char *what() const override { return "TestException"; } }; I used Clion and the IDE give me a warning on the function what() : exception specification of overriding function is more lax than base version But if I build the code with gcc, there's no warning came out. I used c++ 14, gcc 6.5.0 Can anybody help to explain what does the warning mean and can I just ignore it? 回答1: what from std

Why finally block may not execute when exception is thrown?

允我心安 提交于 2020-02-23 08:32:19
问题 For a long time I thought that it allows me to free up all the resources in the finally block and I thought that if an exception happens in the try block, then the resources will still be free up in the finally block. But that seems not to be the case. I have the following piece of code: using System; public sealed class Program { public static void Main() { try { int zero = 0; int i = 1/zero; } finally { Console.WriteLine("divide by zero"); //the line is never called } } } I never reach the

OperationCanceledException VS TaskCanceledException when task is canceled

核能气质少年 提交于 2020-02-19 09:35:33
问题 The following code creates a task which is being canceled. await expression (case 1) throws System.OperationCanceledException while synchronous Wait() (case 2) throws System.Threading.Tasks.TaskCanceledException (wrapped in System.AggregateException ). using System; using System.Threading; using System.Threading.Tasks; public class Program { public static void Main() { Program.MainAsync().Wait(); } private static async Task MainAsync() { using(var cancellationTokenSource = new

junit testing - assertEquals for exception

爷,独闯天下 提交于 2020-02-18 13:55:50
问题 How can I use assertEquals to see if the exception message is correct? The test passes but I don't know if it hits the correct error or not. The test I am running. @Test public void testTC3() { try { assertEquals("Legal Values: Package Type must be P or R", Shipping.shippingCost('P', -5)); } catch (Exception e) { } } The method being tested. public static int shippingCost(char packageType, int weight) throws Exception { String e1 = "Legal Values: Package Type must be P or R"; String e2 =

PL/SQL exception handling: do nothing (ignore exception)

为君一笑 提交于 2020-02-18 05:43:19
问题 This is a question I am asked very frequently. Since I couldn't find any exact duplicate on stackoverflow, I thought I'd post it as a reference. Question: In PL/SQL, I know how to catch exceptions and execute code when they are caught, and how to propagate them to the calling block. For example, in the following procedure, the NO_DATA_FOUND exception is handled directly, while all other exceptions are raised to the calling block: CREATE OR REPLACE PROCEDURE MY_PROCEDURE() IS BEGIN do_stuff();

PL/SQL exception handling: do nothing (ignore exception)

我只是一个虾纸丫 提交于 2020-02-18 05:42:12
问题 This is a question I am asked very frequently. Since I couldn't find any exact duplicate on stackoverflow, I thought I'd post it as a reference. Question: In PL/SQL, I know how to catch exceptions and execute code when they are caught, and how to propagate them to the calling block. For example, in the following procedure, the NO_DATA_FOUND exception is handled directly, while all other exceptions are raised to the calling block: CREATE OR REPLACE PROCEDURE MY_PROCEDURE() IS BEGIN do_stuff();