D
D
daMage2014-08-31 07:44:53
Java
daMage, 2014-08-31 07:44:53

Is it possible to organize a single entry point for exceptional situations?

Hello. It is very annoying when you need to write try-catch in every function where I work with the database. Is it possible to create a single entry point for the entire application and catch the necessary exceptions there?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lookid, 2014-08-31
@lookid

No, it's better not to do that. This will work if you have 100 lines of code. And if 100,000, then it doesn’t roll anymore. In fact, this is such a junior student approach that everything can be solved with one function. Better find a better solution for yourself.

B
bimeg, 2014-08-31
@bimeg

If you really want to, then in the 8th Java you can try to pervert.
It just looks like you have to do a lot of Try*<T> for methods with a specific set of arguments.

public class TryCatch
{
  @FunctionalInterface
  public interface Try<T>
  {
    T trai() throws Exception;
  }

  @FunctionalInterface
  public interface TryDouble<T>
  {
    T trai(Double d) throws Exception;
  }

  @FunctionalInterface
  public interface Catch<T>
  {
    T katch(Exception e);
  }

  public static <T> T trai(TryDouble<T> call, Double d)
  {
    return trai(call, TryCatch::katch, d);
  }

  public static <T> T trai(TryDouble<T> call, Catch<T> katch, Double d)
  {
    try
    {
      return call.trai(d);
    }
    catch (Exception e)
    {
      return katch.katch(e);
    }
  }

  public static <T> T trai(Try<T> call)
  {
    return trai(call, TryCatch::katch);
  }

  public static <T> T trai(Try<T> call, Catch<T> katch)
  {
    try
    {
      return call.trai();
    }
    catch (Exception e)
    {
      return katch.katch(e);
    }
  }

  public static <T> T katch(Exception exception)
  {
    exception.printStackTrace();

    return null;
  }
}

public class Main
{
  public static void main(String[] args)
  {
    Integer i = TryCatch.trai(Main::meth1);
    Double d = TryCatch.trai(Main::meth2);

    System.out.println(i);
    System.out.println(d);

    Double d1 = TryCatch.trai(Main::meth2, (e) -> 2.0);
    Double d2 = TryCatch.trai(Main::meth2, Main::myKatch);
    Double d3 = TryCatch.trai(Main::meth2, (e) ->
    {
      e.printStackTrace();
      return null;
    });

    System.out.println(d1);
    System.out.println(d2);
    System.out.println(d3);

    Double d4 = TryCatch.trai(Main::meth3, 1.0);

    System.out.println(d4);

    Double d5 = TryCatch.trai(Main::meth2, (e) ->
    {
      throw new RuntimeException(e);
    });

    System.out.println(d5);
  }

  public static Integer meth1() throws Exception
  {
    return 10;
  }

  public static Double meth2() throws Exception
  {
    throw new Exception();
  }

  public static Double meth3(Double d) throws Exception
  {
    return d;
  }

  public static <T> T myKatch(Exception exception)
  {
    System.out.println("My Catch!");

    return null;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question