e1084. 捕获错误和异常
All errors and exceptions extend from Throwable. By catching Throwable, it is possible to handle all unexpected conditions.
There are several scenarios where it is good practice to catch Throwable. For example, in a server application, the threads that handle requests should catch Throwable and relay any errors or exceptions to the client. Another scenario is a long-running thread that performs some background activity. Such threads should catch Throwable, log any errors or exceptions, and then continue functioning.
It is rarely good practice for a method in a library to catch Throwable. In general, errors and exceptions should not be masked from the caller.
This example demonstrates a long-running thread that catches Throwable and logs the exception.
class BgThread extends Thread {
// Create a logger. For more information on the logging api's,
// see e385 一个精简的日志记录程序
Logger logger = Logger.getLogger("com.mycompany.mypackage");
BgThread() {
// As a daemon thread, this thread won't prevent the application from exiting
setDaemon(true);
}
// Set to true to shut down this thread
boolean stop = false;
public void run() {
while (!stop) {
try {
// Perform work here
} catch (Throwable t) {
// Log the exception and continue
logger.log(Level.SEVERE, "Unexception exception", t);
}
}
}
}
e1084. 捕获错误和异常的更多相关文章
- PHP错误与异常
请一定要注意,没有特殊说明:本例 PHP Version < 7 说起PHP异常处理,大家首先会想到try-catch,那好,我们先看一段程序吧:有一个test.php文件,有一段简单的PHP程 ...
- Python错误和异常学习
一:错误解释 1.语法错误:代码不符合解释器或者编译器语法 2.逻辑错误:不完整或者不合法输入或者计算出现问题 代码运行前的语法或者逻辑错误,语法错误在执行前修改,逻辑错误无法修改 二:异常 执行过程 ...
- 如何捕获access violation异常
文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...
- PHP 错误与异常 笔记与总结(12 )异常
① 异常的概念:异常和错误的区别 PHP 部分借鉴了 C++ 和 JAVA 中的异常处理机制.PHP 中的异常是指 程序运行和预期不太一致,与错误是两个不同的概念. ② 异常的语法结构 [例1] &l ...
- Android UncaughtExceptionHandler,捕获错误
最近在做个项目,需要在程序出现运行时异常和错误导致程序crash时进行一些操作,找到一个方法 Thread.setDefaultUncaughtExceptionHandler(new Uncaugh ...
- Go语言项目的错误和异常管理 via 达达
Go语言项目的错误和异常管理 最近连续遇到朋友问我项目里错误和异常管理的事情,之前也多次跟团队强调过错误和异常管理的一些概念,所以趁今天有动力就赶紧写一篇Go语言项目错误和异常管理的经验分享. 首先我 ...
- Python学习笔记七-错误和异常
程序员总是和各种错误打交道,学习如何识别并正确的处理程序错误是很有必要的. 7.1错误和异常 1.错误 从软件方面来看,错误分为语法错误和逻辑错误两种.这两种错误都将导致程序无法正常进行下去,当Pyt ...
- WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作)
WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作) 好吧,还是那个社区APP,非管理系统,用户行为日志感觉不是很必要的,但是,错误日 ...
- php错误及异常捕捉
原文:php错误及异常捕捉 在实际开发中,错误及异常捕捉仅仅靠try{}catch()是远远不够的. 所以引用以下几中函数. a) set_error_handler 一般用于捕捉 E_NOTI ...
随机推荐
- C#--索引
索引是一组get和set访问器,类似于属性的访问器. 索引和属性在很多方面是相似的. 和属性一样,索引不用分配内存来存储: 索引和属性都主要被用来访问其他数据成员,这些成员和他们关联,他们为这些成员提 ...
- 转 MySQL中的共享锁与排他锁
原文链接在MySQL中的行级锁,表级锁,页级锁中介绍过,行级锁是Mysql中锁定粒度最细的一种锁,行级锁能大大减少数据库操作的冲突.行级锁分为共享锁和排他锁两种,本文将详细介绍共享锁及排他锁的概念.使 ...
- ps钢笔工具 (重要工具)
ps钢笔工具 ctrl键 + 鼠标选中操作的点 = 移动点 alt键 + 方点 + 拖拉 = 拖出两个手柄 和 控制两端弧度 alt键 + 圆点 = 控制单向弧度
- Linux下adb的配置
进入当前用户主目录 yongdaimi@ubuntu:~$ cd ~ 打开.bashrc文件 yongdaimi@ubuntu:~$ vi .bashrc 在文件末尾添加下列代码 export ADB ...
- linux kernel & source code analysis& hacking
https://kernelnewbies.org/ http://www.tldp.org/LDP/lki/index.html https://kernelnewbies.org/ML https ...
- oracle 存储过程实现数据CURD操作
1.创建数据库表: -- Create table create table TEST_TABLE ( userid NUMBER not null, username NVARCHAR2(50), ...
- cvs history,CVS中查询目录下所有文件的提交记录
我们习惯用eclipse svn中查看一个目录下,甚至整个工程下,所有的文件的变更列表:操作为:右键工程 -> Team -> Show History. 最近在使用CVS,eclipse ...
- URLConnection格式与用法
private void getdialog() { final EditText et = new EditText(this); final String workid = this.workid ...
- webRTC开启摄像头
配置htts之后就可以开启webRTC了. <!DOCTYPE html> <html> <head> <title>OpenCamera</ti ...
- jfinal视频目录
目录-----------------------------------------------------------第一章 简介第二章 JFine2.0 maven demo第三章番外篇 JFi ...