title: Laravel custom exception
body: <p>Before posting this question I have searched internet for appropriate answers but got none.These are my following questions:</p>
  
  <p>1) How to throw exception without try catch in laravel controller and get exception in the view of the called controller.
  <strong>Example : TestController.php</strong></p>
  
  <pre><code>function index(){
  throw new CustomException('Data not found');
  return view('dashboard');
  }
  </code></pre>
  
  <p>How to get exception message in dashboard view</p>
  
  <p>2) How to set format for exception message, suppose I want to return format as</p>
  
  <pre><code>$response['code'] = 0;
          $response['status'] = 'error';
          $response['message'] = 'message';
          $response['data'] = '';
  </code></pre>
  
  <p>I have created a custom exception but don't know how to use it to fully</p>
  
  <pre><code>&lt;?php
  
  namespace App\Exceptions;
  
  use Illuminate\Http\Request;
  use Illuminate\Http\Response;
  use Mockery\Exception;
  
  class CustomException extends Exception{
  
      public $response;
  
  
      /**
       * Report the exception.
       *
       * @return void
       */
      public function report()
      {
      }
  
      /**
       * Render the exception into an HTTP response.
       *
       * @param  \Illuminate\Http\Request
       * @return \Illuminate\Http\Response
       */
      public function render($request,$exception){
          ob_clean();
          $response['code'] = 0;
          $response['status'] = 'error';
          $response['message'] = 'Message';
          $response['data'] = '';
          if(!$request-&gt;ajax()){
              // non ajax response
          }else{
              return response()-&gt;json($response);
          }
      }
  
  }
  </code></pre>