title: Exception Handling in Spring webflux
body: <p>I'm working on a Spring webflux project using reactive streams. I have a usecase as follows and want to know how can it be done in a reactive way.</p>
  <pre><code>  @RestController
    public class Example {
  
      @GetMapping(&quot;/greet&quot;)
      public Mono&lt;String&gt; Test() {
         return Mono.just(&quot;Tim&quot;)
                 .map(s -&gt; s.toUpperCase())
                 .map(s -&gt; s.toLowerCase())
                 .doOnSuccess(s -&gt; validate(s)) // usecase is to validate here in middle of the pipeline
                 .onErrorResume(ex -&gt; Mono.just(&quot;Guest&quot;))
                 .map(s -&gt; &quot;Hi, &quot;+s);
      }
     
    public void validate(String s) {
      if(s.length() &lt; 5) {throw new RuntimeException(&quot;Name is short&quot;);}
    }
   
  }
  </code></pre>
  <p>I know this is a contrived example, but I have something similar to this. I thought throwing an error will lead to exception on browser screen when the endpoint is hit. But to my surprise it went to <code>onErrorResume()</code> and I got <code>Hi, Guest</code> as response. I thought when <code>throw</code> is used to throw an exception before the reactive pipeline is assembled, it will not use <code>onErrorResume()</code>. What am I missing here?</p>
  <p>Also coming to question #2, how can I achieve this if I'm using <code>Mono.error(new RuntimeException(&quot;Name is short&quot;))</code>  instead of <code>throw new RuntimeException(&quot;Name is short&quot;)</code>? Can someone please answer my 2 questions. Suggestions to improve code are appreciated.</p>