+ Start a Discussion

question on test coverage and error handling

i'm trying to figure out how i force an exception to happen so I can cover my catch statements in testMethods.

can anyone tell me how to force a DML exception and a generic one?  i can't figure out how, and when i try to manually set one up, apex just tells me that the exceptions cannot be constructed.
This should get you an DML Exception.
- Create a new object (Task for example) and try to assign it to a user (OwnerId) that doesn't exist in your environment.
- Insert or update the object.
Also, if you're trying to force a System.QueryException, you could construct a SELECT query that doesn't return any rows.