Question:
How to wrap the whole class in throws
?
I just have a lot of methods in the class and in each I do try catch
, the code becomes healthy and not readable.
Is there any way to make the try catch
only be where I create the class object.
Type
try{ new MyClas() }
I'm not strong in Java, don't swear too much
Answer:
Tell your constructor that it can throw an exception:
public class MyClass {
public MyClass() throws Exception { }
}
Now it can only be created in a try block
try{ new MyClas(); } catch(Exception e) {}
All other methods can also be accompanied by throws Exception and wrapping their calls in try – so you won't have to write try blocks inside the methods themselves