Java: Multiple Functions and one ErrorHandler

Let’s say you have function fooA() and function fooB() both functions are very similar and throw similar errors.

 public static void fooA(){
         try{
             throw new FileNotFoundException();
         }
         catch (Exception e){
             exceptionHandler(e);
         }
     }

     public static void fooB(){
         try{
             throw new FileNotFoundException();
         }
         catch (Exception e){
             exceptionHandler(e);
         }
     }

If you have the possibility to avoid duplicating code you could write a single Error handler and pass the Error to the Error handling function. However, you still want to be able to distinguish between different types of Exception and for logging purposes, you should mention which function actually caused Exception. The easiest way to figure the Method name is to use e.getStackTrace()[0].getMethodName(); Alternatively, you can pass the method name to the Errorhandlerfunction. To do that you can figure out the method name via:

 String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();

To determine the various Exceptions there are two possibilities:

Method 1: Use instanceof

The downside to this method is that it is not as readable and identifiable as ErrorHandling function. (However, this depends on personal preferences)

     public static void exceptionHandler(Exception e){
         if (e instanceof FileNotFoundException){
             System.out.println(e + " " + e.getStackTrace()[0].getMethodName());
         } else if (e instanceof Exception) {
             System.out.println(e.getStackTrace()[0].getMethodName());
         }
     }

Method 2: re-throw the Error

Well, you are still throwing around with errors, but it is clearly identifiable as Error handler.

  public static void exceptionHandler(Exception e){
         try {
             throw e;
         }
         catch (FileNotFoundException e){
             System.out.println(e + " " + e.getStackTrace()[0].getMethodName());
         }
         catch(Exception e) {
             System.out.println(e.getStackTrace()[0].getMethodName());
         }
     }

Lenovo Docking-Stations: Automatically Power on Laptop

You start your day out by putting your laptop onto your docking-station and then go get your morning coffee – just to come back and find your laptop still hasn’t booted up because you forgot to press the on the button – frustrated you scream out “Why god? Why do I have to wait another minute to boot my computer?”

This is a good example of a bad user experience design. The user expects the computer to start because the only reason for putting your laptop into a docking station is to start working with the laptop. You expect the device to be smart enough to turn itself on by itself. There is really no reason that the computer should not start. (Eco-friendly Power conservation guys will say, oh no, what a waste of electricity – Well, you are reading this article on a device that is using power and you probably are using a computer for most of your workday)

However, Lenovo laptops have a BIOS setting to automatically power up your laptop.

  1. Start your computer and press the ThinkVantage button (or F1) to open up BIOS
  2. Open Power Settings
  3. Set “Power On with AC Attach” to “Enabled”
  4. Press F10 to Save and Exit (restart)