when a test is deliberately provoking an exception and you want to check the exception, then a try/catch in the test is needed. A common case of this is where you are testing that a trigger is using addError to report an error back to a user:
try {
…
System.assert(false, ‘Exception expected’);
} catch (DMLException e) {
System.assert(e.getMessage().contains(‘expected message’), ‘message=’ + e.getMessage());
}
Note the test should fail if the exception isn’t thrown, hence the System.assert(false, ‘Exception expected’);