Spedwards
Spedwards

Reputation: 4492

Java Throwing Custom Errors

So I've been working with Java for a little while now. I technically am still new to the language.

I have created a couple custom errors for a personal project I'm working on and I was wondering if there was anyway to use the methods that throw these errors without wrapping try-catch blocks around it?

The method that throws these errors gets used a fair bit and each individual statement shouldn't have to be tried.

It's also a problem that anyone who is new to Java and/or someone who doesn't read the documentation won't know about.

Is there any way to run these methods without having to try each one, and what would be the proper way to document these errors?

Upvotes: 2

Views: 2123

Answers (4)

rootExplorr
rootExplorr

Reputation: 575

Java provides 2 types of exceptions : Checked and Unchecked.

Checked once are the those which the compiler will prompt you too handle (in a try-catch block) while for the unchecked exceptions compiler will not ask you to handle them.

RuntimeException class is the top level class for Unchecked exceptions and hence if your custom exception classes extends RuntimeException class then automatically compiler will not prompt you to handle them explicitly in a try-catch block.

Basically RuntimeExceptions are those which java considers as occurring at the execution time and anything occurring at the execution time like NullPointerException cannot be handled, hence we cannot do much at the compile time.

Upvotes: 4

Kevin Workman
Kevin Workman

Reputation: 42174

If you don't want to force your code to wrap these methods in try blocks, why are you having them throw exceptions?

Let's consider these methods:

public void badMethodOne() throws CustomExceptionOne;
public void badMethodTwo() throws CustomExceptionTwo;

You're asking how you can avoid this scenario:

public void myMethod(){
   try{
      badMethodOne();
   }
   catch(CustomExceptionOne ex){
      ex.printStackTrace();
   }

   try{
      badMethodTwo();
   }
   catch(CustomExceptionTwo ex){
      ex.printStackTrace();
   }

}

One option would be to simply catch a superclass Exception:

public void myMethod(){
   try{
      badMethodOne();
      badMethodTwo();
   }
   catch(Exception ex){
      ex.printStackTrace();
   }

}

You might also use a multi-catch block:

public void myMethod(){
   try{
      badMethodOne();
      badMethodTwo();
   }
   catch(CustomExceptionOne | CustomExceptionTwo ex){
      ex.printStackTrace();
   }
}

You might also pass responsibility "up the chain" and let whoever's calling your code handle the try/catch:

public void myMethod() throws CustomExceptionOne, CustomExceptionTwo{
      badMethodOne();
      badMethodTwo();
}

Upvotes: 2

Ya Wang
Ya Wang

Reputation: 1808

You can catch Exception or Throwable

Basically you only need to catch the superclass of the type of exceptions thrown, since most exceptions are a subclass of Exception and Throwable both will catch almost everything.

In addition, RuntimeException don't need to be handled at compile time. One that I am sure you know about is NullPointerExceptions, those are RuntimeExceptions. It's not a good practice to have them in your code but if you must.

The best way is to just handle Exceptions within your code.

Edit: You really only need one try and catch block you can use the throws keyword to pass the exception handling more upstream where you can handle it there in your case probably just one try catch block

Upvotes: -1

Kon
Kon

Reputation: 10810

Make your custom exceptions extend the RuntimeException class. These by definition do not need to be declared as thrown from a method and don't need to be caught by anyone calling that method..

See the Javadocs RuntimeException.

Upvotes: 1

Related Questions