When you have to throw an exception manually, you should use the throw keyword. This gives you the flexibility of throwing an exception as required from any block of code or function. This comes with the constraint that the exception must be of the type, java.lang.Throwable. When you work on big development projects, knowing proper exception handling is quintessential. You can take up a java certification course to get these concepts clear. Here's how 'throw' is implemented:
1 2 3 4 5 6 7 8 9 | class Example { void method() throws Exception { Exception e = new Exception(); throw e; } } |