exception-handling

Why is exception.printStackTrace() considered bad practice?

夙愿已清 提交于 2019-12-17 02:34:00
问题 There is a lot of material out there which suggests that printing the stack trace of an exception is bad practice. E.g. from the RegexpSingleline check in Checkstyle: This check can be used [...] to find common bad practice such as calling ex.printStacktrace() However, I'm struggling to find anywhere which gives a valid reason why since surely the stack trace is very useful in tracking down what caused the exception. Things that I am aware of: A stack trace should never be visible to end

Exception handling in JSF ajax requests

旧时模样 提交于 2019-12-17 02:33:26
问题 How do I handle the exception and access the stack trace when an exception is thrown while processing a JSF ajax request? Right now, I only get the exception class name and message in a JavaScript alert when JSF project stage is set to Development. Even worse, there's no visual feedback whatsoever when JSF project stage is set to Production, and the server log doesn't show any information about the exception. If that's relevant, I'm using GlassFish in Netbeans. 回答1: This problem is known and

What is the correct way to deal with JSF 2.0 exceptions for AJAXified components?

隐身守侯 提交于 2019-12-17 02:31:52
问题 I have set up web.xml so that anything that's java.lang.Throwable (i.e. any uncaught exceptions or errors) will forward to an error page. However, for AJAXified components, exceptions dont get routed to the error page via this mechanism. The test case I have is a simple CommandButton tied to an action method that always throws a RuntimeException . It seems like the best practice would be to have the action method catch the exception and add a FacesMessage of type error severity. Is this what

Exception handling : throw, throws and Throwable

我的未来我决定 提交于 2019-12-17 02:28:49
问题 Can any of you explain what the differences are between throw , throws and Throwable and when to use which? 回答1: throws : Used when writing methods, to declare that the method in question throws the specified (checked) exception. As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException . throw : Instruction to actually throw the exception. (Or more specifically, the Throwable ). The throw

Exception thrown in catch and finally clause

隐身守侯 提交于 2019-12-17 02:21:24
问题 On a question for Java at the university, there was this snippet of code: class MyExc1 extends Exception {} class MyExc2 extends Exception {} class MyExc3 extends MyExc2 {} public class C1 { public static void main(String[] args) throws Exception { try { System.out.print(1); q(); } catch (Exception i) { throw new MyExc2(); } finally { System.out.print(2); throw new MyExc1(); } } static void q() throws Exception { try { throw new MyExc1(); } catch (Exception y) { } finally { System.out.print(3

Exception thrown in catch and finally clause

会有一股神秘感。 提交于 2019-12-17 02:20:09
问题 On a question for Java at the university, there was this snippet of code: class MyExc1 extends Exception {} class MyExc2 extends Exception {} class MyExc3 extends MyExc2 {} public class C1 { public static void main(String[] args) throws Exception { try { System.out.print(1); q(); } catch (Exception i) { throw new MyExc2(); } finally { System.out.print(2); throw new MyExc1(); } } static void q() throws Exception { try { throw new MyExc1(); } catch (Exception y) { } finally { System.out.print(3

C++ catch blocks - catch exception by value or reference? [duplicate]

强颜欢笑 提交于 2019-12-17 02:19:26
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: catch exception by pointer in C++ I always catch exceptions by value. e.g try{ ... } catch(CustomException e){ ... } But I came across some code that instead had catch(CustomException &e) instead. Is this a)fine b)wrong c)a grey area? 回答1: The standard practice for exceptions in C++ is ... Throw by value, catch by reference Catching by value is problematic in the face of inheritance hierarchies. Suppose for your

Spring Boot REST service exception handling

旧巷老猫 提交于 2019-12-17 02:17:17
问题 I am trying to set up a large-scale REST services server. We're using Spring Boot 1.2.1 Spring 4.1.5, and Java 8. Our controllers are implementing @RestController and the standard @RequestMapping annotations. My problem is that Spring Boot sets up a default redirect for controller exceptions to /error . From the docs: Spring Boot provides an /error mapping by default that handles all errors in a sensible way, and it is registered as a ‘global’ error page in the servlet container. Coming from

catch exception by pointer in C++

我与影子孤独终老i 提交于 2019-12-17 02:11:49
问题 I found that there are three ways to catch an exception, what are the differences? 1) catch by value; 2) catch by reference; 3) catch by pointer; I only know that catch by value will invoke two copies of the object, catch by reference will invoke one. So how about catch by pointer? When to use catch by pointer? In addition to throw an object, can I throw a pointer to an object like this? class A {} void f() { A *p = new A(); throw p; } 回答1: The recommended way is to throw by value and catch

Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?

China☆狼群 提交于 2019-12-17 02:09:06
问题 I've long been under the impression that goto should never be used if possible. While perusing libavcodec (which is written in C) the other day, I noticed multiple uses of it. Is it ever advantageous to use goto in a language that supports loops and functions? If so, why? 回答1: There are a few reasons for using the "goto" statement that I'm aware of (some have spoken to this already): Cleanly exiting a function Often in a function, you may allocate resources and need to exit in multiple places