checked-exceptions

How does one decide to create a checked excpetion or an unchecked exception [duplicate]

≯℡__Kan透↙ 提交于 2019-12-20 07:25:03
问题 This question already has answers here : Understanding checked vs unchecked exceptions in Java (21 answers) Closed 3 years ago . I want to know how does one know to create and throw a checked exception or an unchecked exception. For example I have a service which takes some data and validates it before using it. During validation a certain field did not meet the rules and I want to throw an exception say ValidationException(). How will I know of decide it should be checked or unchecked. In

How does JVM handles RuntimeException(s)

北城以北 提交于 2019-12-19 03:23:07
问题 While creating custom exceptions, If we want to create a checked Exception we extend the Exception class and for unchecked exception we extend the RuntimeException class. My question is, how JVM handles subClasses of RuntimeException and Exception differently when they all are sub classes of the Exception class. 回答1: It doesn't. The only difference is in requirements enforced by the compiler. 回答2: You are mistaken that the JVM handles the exceptions differently, but your question is still

declare a method always throws an exception?

女生的网名这么多〃 提交于 2019-12-13 12:24:35
问题 I have a method like... int f() { try { int i = process(); return i; } catch(Exception ex) { ThrowSpecificFault(ex); } } This produces a compiler error, "not all code paths return a value". But in my case ThrowSpecificFault() will always throw (the appropriate) exception. So I am forced to a put a return value at the end but this is ugly. The purpose of this pattern in the first place is because "process()" is a call to an external web service but need to translate a variety of different

Should I use a relevant built-in unchecked exception where the theory prescribes using a checked one?

末鹿安然 提交于 2019-12-11 03:49:02
问题 There are quite a few posts on SO about the "checked vs unchecked exception" topic. This answer is probably the most well-rounded and informative. Yet I'm still conflicted to follow the logic presented there, and there's a reason for that. I'm building a wrapper API around a set of services similar to each other. There are, however, minor differences between them (or a possibility of such in the future), so that certain functions (of secondary and shortcut nature) may be supported by some

Will a subclass catch block catch a checked parent exception?

被刻印的时光 ゝ 提交于 2019-12-10 03:22:36
问题 I have a method with a checked exception for a parent class, which can throw exceptions of the type parent and subclass public void method() throws ParentException { if( false ) throw new ParentException(); else if( true ) throw new ChildException(); // this one is thrown } and I have a cascade catch block, which first has the child exception try { method(); } catch (ChildException e) { // I get here? } catch (ParentException e) { // or here? } Which block will catch the exception thrown?

Managing checked exceptions in different JUnit tests

99封情书 提交于 2019-12-07 20:45:09
问题 I am writing a Java Unit test for one of my method. The method declaration is like this: public int convertToInteger() throws InvalidRomanNumberException { int result=0; BaseRomanNumeral num1, num2; int i=0; if(!validOperation()) throw new InvalidRomanNumberException(); } Now I am trying to write two unit tests. One is to test if the right exception is thrown. Another one is to make sure that that the write conversion happens. This is how my test case looks @Test public void

How does scala generated byte code drops the checked exception?

二次信任 提交于 2019-12-06 03:25:12
问题 If it possible to write byte code for a method that is supposed to throw a checked exception? For instance the following Java class doesn't compile unless the method declares it throws the checked exception: public class CheckedExceptionJava { public Class<?> testChecked(String s) throws ClassNotFoundException { return Class.forName(s); } } While the following Scala equivalent does ( because Scala doesn't have checked exceptions ) : class CheckedException { def testChecked( s : String ) =

Will a subclass catch block catch a checked parent exception?

萝らか妹 提交于 2019-12-05 03:34:03
I have a method with a checked exception for a parent class, which can throw exceptions of the type parent and subclass public void method() throws ParentException { if( false ) throw new ParentException(); else if( true ) throw new ChildException(); // this one is thrown } and I have a cascade catch block, which first has the child exception try { method(); } catch (ChildException e) { // I get here? } catch (ParentException e) { // or here? } Which block will catch the exception thrown? Since the method declares explicitly the ParentException only, would the ChildException be shown as an

declare a method always throws an exception?

故事扮演 提交于 2019-12-04 22:20:19
I have a method like... int f() { try { int i = process(); return i; } catch(Exception ex) { ThrowSpecificFault(ex); } } This produces a compiler error, "not all code paths return a value". But in my case ThrowSpecificFault() will always throw (the appropriate) exception. So I am forced to a put a return value at the end but this is ugly. The purpose of this pattern in the first place is because "process()" is a call to an external web service but need to translate a variety of different exceptions to match a client's expected interface (~facade pattern I suppose). Any cleaner way to do this?

How to identify checked and unchecked exceptions in java?

一曲冷凌霜 提交于 2019-12-04 16:45:32
问题 While reading about exception, I will always come across checked exceptions and unchecked exceptions, So wanted to know how to distinguish that which is what? Edit: I want to know if i create any exception class then how can i create as a checked or as an unchecked? and what is the significance of each? 回答1: All Throwable s except subclasses of java.lang.RuntimeException or java.lang.Error are checked. Properly, in Java, "exceptions" are subclasses of java.lang.Exception , "errors" are