In the following scenario, I tried to understand how to handle this code, and how it relates to a Runtimexception. I read that it is generally better to throw exceptions at runtime rather than relying on static exceptions. And it might even be better to catch the static checked exception and throw the excluded exception.
Are there any scenarios in which a static exception can be detected, possibly a catch-all Exception, and just handle the exception. It is possible to register an error message and continue.
In the code below, in the execute1 and execute2 method, let's say there is mutable code, do you detect a static exception and then retronize? Or maybe there are other errors:
if (null == someObj) {throw new RuntimeException (); }
Is this approach used?
Pseudocode:
public class SomeWorkerObject { private String field1 = ""; private String field2 = ""; public setField1() { } public setField2() { } // Do I throw runtime exception here? public execute1() { try { // Do something with field 1 // Do something with field 2 } catch(SomeException) { throw new RuntimeException(); } } // Do I throw runtime exception here? public execute2() { try { // Do something with field 1 // Do something with field 2 } catch(SomeException) { throw new RuntimeException(); } } } public class TheWeb { public void processWebRequest() { SomeWorkerObject obj = new SomeWorkerObject(); obj.setField1("something"); obj.setField2("something"); obj.execute1(); obj.execute2(); // Possibility that runtime exception thrown? doSomethingWith(obj); } }
I have a couple of problems with this code. There are times when I do not want a run-time exception to be triggered because execution stops in the calling method. It seems that if I catch errors in the method, maybe I can continue. But I will know if I can continue to work on the program.
In the above example, what if obj.execute1 () throws a Runtimeexception, then does the code exit?
Edited: This guy seems to be answering many of my questions, but I still want to hear your opinions.
http://misko.hevery.com/2009/09/16/checked-exceptions-i-love-you-but-you-have-to-go/
" catch, : , , ".
RuntimeException RuntimeException. .
throw new RuntimeException(originalException).
throw new RuntimeException(originalException)
, .
, , .
:
try { hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { // Unexpected exception. "MD5" is just hardcoded and supported. throw new RuntimeException("MD5 should be supported?", e); } catch (UnsupportedEncodingException e) { // Unexpected exception. "UTF-8" is just hardcoded and supported. throw new RuntimeException("UTF-8 should be supported?", e); }
, runtimeexception, . , , , . , .
. RuntimeException , , try-block 'throws'.
, , . , .
- , . . finally.
finally
-, . , "" , , "", .
, , -, , , , isn ' .
, , . , , . , (, NumberFormatException ). , .
NumberFormatException
, , , , RuntimeException. , , , .
RuntimeException
, , . , -, IOException - . , , . , -, , , , , . API , -, , , , , , . RuntimeException, , , , .
IOException
Source: https://habr.com/ru/post/1723513/More articles:OpenGL AntiAliasing and Background Quad - backgroundhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1723509/how-to-restrict-asmx-web-services-to-be-ssl-only&usg=ALkJrhjUqTVKXPA42ujtT_GIIK3UTGr0YwCSS Cover image text with image - html.NET Point.IsEmpty vs IsDefined - c #How do you globally change page output sent from IIS without changing the page source? - securityПакетный файл для удаления папок - dosHow to count and display objects with respect to ManyToMany in Django - pythonReducing the CUDA of many small, unevenly sized arrays - arraysJNA sample program java.lang.NoClassDefFoundError - javamingw + python нетерпеливо переводя путь - pythonAll Articles