exception-handling

Why is it best to not catch exceptions in general-purpose classes?

我与影子孤独终老i 提交于 2019-12-23 21:00:51
问题 When you implement a class for some general-purpose use, why is it best not to catch exceptions? I suppose it's so that the error can go up the stack and help in the debugging process. Any other reasons? 回答1: This rule should better read: Do not handle Exceptions you do not know how to handle! If e.g. you write a class that reads a CSV file and return the tokens of the line, you will have some points in your class where an IOException might be thrown. You should definitely not catch it,

How do I handle exceptions in a procedural language?

独自空忆成欢 提交于 2019-12-23 20:30:14
问题 How do I do exception handling in a procedural language like C or Perl? (I know Perl also does OO.) What’s the best way to handle exception in procedural code in Perl? 回答1: In Perl 5 the exception handling is done using eval and die . You simply eval the body of code and if it dies, you can inspect $@ for the error. It’s not exactly this easy if you want to do it properly, which is why the various try/catch modules exist. You might be interested in Try::Tiny which has no dependencies and

How to insert a new record which has a auto increment number as primary key?

删除回忆录丶 提交于 2019-12-23 20:26:51
问题 For example, I have the following Table: CustomerInfo cus_id: auto increment, int, is Identity cus_name: nvarchar If I use the follow codes to insert the record "Peter", string name = "Peter"; DataContext DC = new DataContext(); CustomerInfo newCustomer = new CustomerInfo(); newCustomer.cus_name = name; DC.CustomerInfos.InsertOnSubmit(newCustomer); DC.SubmitChanges(); The following error returns, Can't perform Create, Update, or Delete operations on 'Table(CustomerInfo)' because it has no

Handling Runtime exception in Java

和自甴很熟 提交于 2019-12-23 20:25:15
问题 I have the following piece of code try{//do something } catch (Exception e) { log.error(e, e); if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new RuntimeException(e); } } the findbugs stataic analysis tool throws this warning on it instanceof will always return true for all nonnull values in methodX, since all RuntimeException are instances of RuntimeException what i dont understand is that its Exception which is being caught and not the RuntimeException, so

Eclipse. Why don't I see compile error if method with unreachable code doesn't invoke?

纵饮孤独 提交于 2019-12-23 20:14:20
问题 I noticed that if method with unreachable code doesn't invoke - then this code compiles by eclipse compiler and executes. Demonstration: import java.io.FileNotFoundException; class R3 { public void g(){ try { } catch (FileNotFoundException e) {//any checked exception } } public static void main(String [] args) { System.out.println("23"); new R3().g(); } } result: Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body and compare it with

How to handle Zend_Exception's in preDispatch or init of an action when using a base controller?

六眼飞鱼酱① 提交于 2019-12-23 19:38:54
问题 As mentioned in the Zend Framework manual, I created a base controller. Subclassing the Action Controller By design, Zend_Controller_Action must be subclassed in order to create an action controller. At the minimum, you will need to define action methods that the controller may call. Besides creating useful functionality for your web applications, you may also find that you're repeating much of the same setup or utility methods in your various controllers; if so, creating a common base

How to log Client Side exceptions in StackExchange.Exceptional?

落花浮王杯 提交于 2019-12-23 18:44:56
问题 I am working on a web project and implemented StackExchange.Exceptional for server exceptions logging. I am just wondering if I could log my client side exceptions as well through StackExchange.Exceptional . Is there any way to use this library for client-side exceptions logging? 回答1: As far as client side logging is concerned there is no mechanism as per my knowledge. Because the developer never maintain any database of stuff on client side. Even if you save exception in client side storage

Creating a custom exception class and custom handler class in Laravel 5.3

徘徊边缘 提交于 2019-12-23 18:34:20
问题 Before I get to the code, let me explain my aim. My web app displays vehicles for sale. I have a need for a custom 404 page that will display 12 of the latest vehicles added to the database if the user tries to access a page that doesn't exist. I have the following... App\Exceptions\CustomException.php <?php namespace App\Exceptions; use Exception; class CustomException extends Exception { public function __construct() { parent::__construct(); } } App\Exceptions\CustomHandler.php <?php

Python Exception Handling - Best Practices

﹥>﹥吖頭↗ 提交于 2019-12-23 18:27:34
问题 I'm writing a python program the accesses a database. I want to catch three types of exceptions when I make a http request. Timeouts, network errors, and http errors. I'm looking for the best way to deal with this situation. I need to check for these exceptions multiple times in multiple areas of my code, and it will look something like this each time: try: //some request except timeout: print '\nException: Timeout Error' except connection error: print '\nException: Network Error' except http

String.format using a exception.getMessage() as a format

喜你入骨 提交于 2019-12-23 17:35:15
问题 I have a question related to String.format in JAVA. My HibernateDao Class is responsible for persisting entities and will throw an exception in case I have any constrain violation. The message contains a %s and will be used as a format in the upper layers, as I should be worried about types in this layer, thus can't identify what object I could not persist. public Entity persistEntity(Entity entity) { if (entity == null || StringUtils.isBlank(entity.getId())) throw new InternalError(CANNOT