51939

Java what is the benefit of Throwing RuntimeException

What is the benefit of declaring (Unchecked Exception)throws exception along method signature b/c it does not force caller to keep in try catch block.

public void testRuntimeEx()throws RuntimeException{ if(1==1){throw new RuntimeException()} } //Caller method public void testCaller(){ // not necessery to handle even caller does not known which RuntimeException might be throws then what is the benefit throws clause with method signature testRuntimeEx(); }

Answer1:

It still serves as documentation, especially if you do not use a generic RuntimeException, but something more specific like IllegalArgumentException or UnsupportedOperationException or IndexOutOfBoundsException and also add some JavaDoc as to when that would happen.

In your example code snippet, it is pretty meaningless, though.

Answer2:

This declaring is a signal for developer who uses this code, that it throws RuntimeException. But it doesn't smell good.

P.S. Code you posted will not compile:

throw RuntimeException

This is not correct throwing.

Answer3:

The benefit is that typically caller does not have anything to do with the exception. It catches it, wraps with other exception and throws again. Or, alternatively, declares the exception using throws keyword and becomes transparent for this type if exception.

I'd say that the described situation is typical for applications where we usually write business code and have one centralized place that handles all exceptions. It is not correct for APIs. For example if you are using library that implements SSH you are expecting it to throw IOException (or even more specialize exception) when something is going wrong.

Answer4:

if you throw custom unchecked exception it becomes more meaningfull rather catching system exception and unchecked exception does not force to catch too.

Recommend

  • Java: sl4j how to log for all classes?
  • What is the use of the SHT_NULL section in ELF?
  • Why cygnus crashes when using ngsiv2 in orion?
  • WPF Listbox commands
  • Azure Service Bus topics partitioning
  • Sum values in array of hash if they have the same value
  • Program crashes when run outside IDE
  • Why is OpenID Connect considered mobile friendly compared to SAML
  • JavaScript overloading with a callback
  • dismiss Progress Dialog in another Activity … Android
  • How to override __call__ in celery on main?
  • Get the last date of each month in a list of dates in Python
  • Access the state of control in Winforms from another application
  • How to open multiple instances of a program in Linux
  • Android - Calling Java from Webview
  • How to display a tooltip according to mouse position? - JavaFX
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Android onKey w/ virtual keyboard
  • HttpServletRequest getLocale returns OS locale not browser locale
  • Trying to string.Join an IList
  • Clear fused location provider's location for testing
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Remove final comma from string in vb.net
  • x64 applications using gdi+: what are the consequences on performance?
  • Django rest serializer Breaks when data exists
  • How to do unit test for HttpContext.Current.Server.MapPath
  • How to rebase a series of branches?
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Azure Cloud Service Web Role web pages do not load
  • Excel - Autoshape get it's name from cell (value)
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Java applet as stand-alone Windows application?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Jquery - Jquery Wysiwyg return html as a string
  • Matrix multiplication with MKL
  • SQL merge duplicate rows and join values that are different
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Reading document lines to the user (python)
  • Converting MP3 duration time