Home > Blockchain >  Calling a method with exceptions in another class
Calling a method with exceptions in another class

Time:03-01

I have a class with many methods which can generate problems, so I have implemented exception handling for these. Now I want to use these methods in another class.

Do I need to handle these exceptions again with a try and catch or do I only need to call the method and I am finished?

CodePudding user response:

you can create a global exception handler with custom exceptions that handles all the exceptions implementations and you only throw the exceptions were you want. So you don't need to implement the exception every time

CodePudding user response:

Your question is not easy to answer because it is a bit vague. By saying "I have implemented exception handling for these" its not clear whether

  1. you are saying that your methods are handling potential exceptions internally in a try catch block and are not propagating these exceptions further down the call chain
  2. or you are saying that all your methods can potentially throw checked exceptions

If it is option no 1. then you don't have to do any explicit exception handling in the rest of your call chain. You can simply call the methods. (if of course your methods handle the exceptions gracefully and do not need any other explicit clean up logic further down the road.)

If it is option no 2. and your exceptions are checked then you are forced to implement some kind of exception handling somewhere in the call chain that is using your methods.

  • Related