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. 捕获错误和异常的更多相关文章

  1. PHP错误与异常

    请一定要注意,没有特殊说明:本例 PHP Version < 7 说起PHP异常处理,大家首先会想到try-catch,那好,我们先看一段程序吧:有一个test.php文件,有一段简单的PHP程 ...

  2. Python错误和异常学习

    一:错误解释 1.语法错误:代码不符合解释器或者编译器语法 2.逻辑错误:不完整或者不合法输入或者计算出现问题 代码运行前的语法或者逻辑错误,语法错误在执行前修改,逻辑错误无法修改 二:异常 执行过程 ...

  3. 如何捕获access violation异常

    文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...

  4. PHP 错误与异常 笔记与总结(12 )异常

    ① 异常的概念:异常和错误的区别 PHP 部分借鉴了 C++ 和 JAVA 中的异常处理机制.PHP 中的异常是指 程序运行和预期不太一致,与错误是两个不同的概念. ② 异常的语法结构 [例1] &l ...

  5. Android UncaughtExceptionHandler,捕获错误

    最近在做个项目,需要在程序出现运行时异常和错误导致程序crash时进行一些操作,找到一个方法 Thread.setDefaultUncaughtExceptionHandler(new Uncaugh ...

  6. Go语言项目的错误和异常管理 via 达达

    Go语言项目的错误和异常管理 最近连续遇到朋友问我项目里错误和异常管理的事情,之前也多次跟团队强调过错误和异常管理的一些概念,所以趁今天有动力就赶紧写一篇Go语言项目错误和异常管理的经验分享. 首先我 ...

  7. Python学习笔记七-错误和异常

    程序员总是和各种错误打交道,学习如何识别并正确的处理程序错误是很有必要的. 7.1错误和异常 1.错误 从软件方面来看,错误分为语法错误和逻辑错误两种.这两种错误都将导致程序无法正常进行下去,当Pyt ...

  8. WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作)

    WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作) 好吧,还是那个社区APP,非管理系统,用户行为日志感觉不是很必要的,但是,错误日 ...

  9. php错误及异常捕捉

    原文:php错误及异常捕捉 在实际开发中,错误及异常捕捉仅仅靠try{}catch()是远远不够的. 所以引用以下几中函数. a)   set_error_handler 一般用于捕捉  E_NOTI ...

随机推荐

  1. Angularjs 控制器controller的作用

    我们在view中给模型的一个参数name赋值 “hello world” . 这是一种简单的赋值,我们可以在视图中通过 ng 指令(以ng-开头的指令)实现了简单的赋值,如果遇到复杂的逻辑运算操作,那 ...

  2. Litjson序列化

    var jsonStr = JsonMapper.ToJson(tmpType); var tmpObject = JsonMapper.ToObject<TestClass>(jsonS ...

  3. 【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现

    分类:C#.Android.VS2015: 创建日期:2016-02-21 一.简介 实际上,对于布局文件中的View来说,大多数情况下,Android都会自动保存这些状态,并不需要我们都去处理它.这 ...

  4. db papers

    http://www.redbook.io/ http://db.cs.berkeley.edu/papers/ https://github.com/rxin/db-readings http:// ...

  5. javascript原生bind方法ie低版本兼容详解

    上一篇文章讲到了javascript原生的bind方法: http://www.cnblogs.com/liulangmao/p/3451669.html 这篇文章就在理解了原生bind方法的原理以后 ...

  6. centos 7 禁用笔记本触摸板设置

    安装 xorg-x11-apps 包: yum install xorg-x11-apps. 终端输入 xinput list 找到 PS/2 记录后面的ID xinput list ⎡ Virtua ...

  7. iOS开发之地域选择

    代码地址: https://github.com/boundlessocean/AreaPickViewDome.git 效果:   第一步: 将BLAreaPickerView 导入工程     第 ...

  8. cocos2d-x 3.0点击响应

    迄今为止,发现cocos2d-x 3.0最让人惊艳的地方就是更改了点击事件机制.(ps:迄今只看了点击事件这块,捂嘴笑~~~) cocos2d-x 2.0 只有CCLayer有点击事件处理,需要注册, ...

  9. 关于Solr6.0中solrj使用简单例子

    solr6.0的solrJ接口有部分变化,下面列出了简单的使用实例,有需要的朋友可以参考下. package com.ailk.solr6; import java.io.IOException; i ...

  10. Hdu1163 Eddy's digitai Roots(九余数定理)

    题目大意: 给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下: 例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6: 因为 6 < 10,所 ...