0

I'm new to Java custom exceptions. I see many examples of how to create them, but there are two questions I cannot find answers to:

1) Can a custom exception be defined within the object class it's intended for, or must it be in its own class file?

2) Regardless of location, can multiple custom exceptions be defined within a single class file, or does every custom exception require its own class file?

Some code snippets would be greatly appreciated.

Thanks.

spinjector
  • 3,121
  • 3
  • 26
  • 56
  • For 1) and 2), do you mean "custom exception class" or "custom exception object"? I ask because one could customize a thrown exception by setting its message or another field to a customized value prior to throw. – Mark A. Fitzgerald May 15 '16 at 17:23
  • @MarkA.Fitzgerald The message can be customized but the exception thrown stays the same unfortunately. – Yassin Hajaj May 15 '16 at 17:35

5 Answers5

3

Exceptions are classes, so obviously they each need their own class. It can be a normal class, inner class or a nested class as usual.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

1) Yes:

public class Entity
{
   public void foo() throws EntityException
   {
      ...
      throw new EntityException();
   }

   public static class EntityException extends Exception
   {
   ...
   }
}

2) You can wrap each custom exception in a container class like this:

public class CustomExceptions
{
   public static class CustomExceptionA extends Exception
   {
   ...
   }

   public static class CustomExceptionB extends Exception
   {
   ...
   }
}
beosign
  • 433
  • 5
  • 10
0

As exception are classes so, some code snippet are given below:

Java Custom Exception:

InvalidAgeException.java

class InvalidAgeException extends Exception{  
 InvalidAgeException(String s){  
  super(s);  
 }  
} 

TestCustomException1.java

class TestCustomException1{  

   static void validate(int age)throws InvalidAgeException{  
     if(age<18)  
      throw new InvalidAgeException("not valid");  
     else  
      System.out.println("welcome to vote");  
   }  

   public static void main(String args[]){  
      try{  
      validate(13);  
      }catch(Exception m){System.out.println("Exception occured: "+m);}  

      System.out.println("rest of the code...");  
  }  
} 

Output:

Output:Exception occured: InvalidAgeException:not valid
       rest of the code...
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
0

Here are examples relative to your question.

An Exception, like a normal class, can be nested, inner, anonymous or top-level.

There's is a rule that does need to be followed though : checked exceptions are subclasses of Exception while non checked are subclasses of RunTimeExceptions (even though RTE are Exceptions, this is the way to go)


Source

public class MyException extends Exception{

    public MyException (String msg) {
        super(msg);
    }

}

class DifferentExceptions {

    class InnerException extends Exception {
        public InnerException(String msg) {
            super(msg);
        }
    }

    static class NestedException extends Exception {
        public NestedException(String msg) {
            super(msg);
        }
    }

    public static void main(String[] args) throws Exception {
        int example = 0;

        switch(example) {
        case 1 : throw new MyException("msg") ; 
        case 2 : throw new DifferentExceptions().new InnerException("msg") ; 
        case 3 : throw new DifferentExceptions.NestedException("msg") ; 
        default : throw new Exception("Not found") ;
        }
    }

}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

1) A custom exception class can be defined either within the class it is intended for or in a separate class.

Example of the former - ThrowingClass.java:

public class ThrowingClass {
    public static class ThrownInnerException extends Exception {
        public ThrownInnerException() {};
    }

    public void throwingMethod() throws ThrownInnerException {
        throw new ThrownInnerException();
    }
}

Example of the latter - ThrownException.java:

public class ThrownException extends Exception {
    public ThrownException() {};
}

and ThrowingClass.java:

public class ThrowingClass {
    public void throwingMethod() throws ThrownException {
        throw new ThrownException();
    }
}

2) Multiple custom exception classes can be defined within a single class file. Each custom exception class does not require its own class file.

Example - MultiThrowingClass.java:

public class MultiThrowingClass {
    public static class ThrownExceptionTypeOne extends Exception {
        public ThrownExceptionTypeOne() {};
    }

    public static class ThrownExceptionTypeTwo extends Exception {
        public ThrownExceptionTypeTwo() {};
    }

    public void throwingMethodOne() throws ThrownExceptionTypeOne {
        throw new ThrownExceptionTypeOne();
    }

    public void throwingMethodTwo() throws ThrownExceptionTypeTwo {
        throw new ThrownExceptionTypeTwo();
    }
}
Mark A. Fitzgerald
  • 1,249
  • 1
  • 9
  • 20