title: Exceptions in Java
body: <p>At the moment I am developing a website while using the Playframework2. I am just a beginner in programming. I read some books about exceptions but now in the real world , exception handling is really strange.</p>
  
  <p>To be honest I don't really care what exceptions are thrown I handle all exceptions the same way.
  <code>return badrequest();</code> . I only use exceptions for logging.</p>
  
  <pre><code>try{
  ...
  }
  catch(Exeption e){
  //log
  return badrequest();
  }
  </code></pre>
  
  <p>But this is so much boilerplate and it's really annoying to write, because every method throws the same exceptions.</p>
  
  <p>Any tips , hints or resources that you could give me?</p>
  
  <p>edit:</p>
  
  <p>An example would be my "global" config file. Because I need to connect to the db every time I thought i could write a singleton for this problem.</p>
  
  <pre><code>private Datastore connect() throws UnknownHostException, MongoException,
              DbAuthException {
  
          Mongo m = new Mongo(dbUrl, dbPort);
          Datastore ds = new Morphia().createDatastore(m, dbName);
          boolean con = ds.getDB().authenticate(username, password.toCharArray());
          if (!con)
              throw new DbAuthException();
          return ds;
      }
  </code></pre>
  
  <p>This also results in a try and catch every time I want to connect to the db. My problem is I don't think I can handle them diffidently.</p>
  
  <p>A code example :</p>
  
  <pre><code>public static Result addComment(String title) {
          try {
  
              Datastore ds = DatabaseConnect.getInstance().getDatastore();
              Form&lt;Comment&gt; filledForm = commentForm.bindFromRequest();
              Comment userComment = filledForm.get();
              userComment.setUsername(Util.getUsernameFromSession(ctx()));
              User.increasePointsBy(ctx(), 1);
              UserGuides.addComment(title, userComment);
          } catch (Exception e) {
              return badRequest();
          }
          return redirect(routes.Guides.blank());
      }
  </code></pre>
  
  <p>In this case I was to lazy to write the same try and catch over and over again, and this is duplicated code.</p>
  
  <p>Maybe there is a book that explains how to design a big application with exception handling?</p>