BAEL-3397: Difference between throw e and throw new Exception(e) (#8339)
* Article: Quick and practical example of hexagonal architecture in java with Spring Project * Removed server.port property from application.properties * BAEL-3397: Difference between throw e and throw new Exception(e) in java * BAEL-3397 : Removed links from readme file * BAEL-3397: removed hexagonal module from the code * BAEL-3397: renamed exceptions package name to rethrow
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.rethrow;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.baeldung.rethrow.custom.InvalidDataException;
|
||||
|
||||
public class RethrowDifferentExceptionDemo {
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(RethrowDifferentExceptionDemo.class.getName());
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String name = null;
|
||||
|
||||
try {
|
||||
|
||||
// Below line will throw NullPointerException
|
||||
if (name.equals("Joe")) {
|
||||
// Do blah blah..
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "So and so user is unable to cast vote because he is found uneligible");
|
||||
throw new InvalidDataException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.rethrow;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RethrowSameExceptionDemo {
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(RethrowDifferentExceptionDemo.class.getName());
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String name = null;
|
||||
|
||||
try {
|
||||
|
||||
// Below line will throw NullPointerException
|
||||
if (name.equals("Joe")) {
|
||||
// Do blah blah..
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "Exception occurred due to invalid name");
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.rethrow.custom;
|
||||
|
||||
public class InvalidDataException extends Exception {
|
||||
|
||||
public InvalidDataException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user