function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alibzafaralibzafar 

Throw an exception Error

Hi, 

 

I am receiving  this  error in my test class , when I want to throw exception in try {}. 

 

Here is my code 

 

@isTest(seeAllData = false) 
public with sharing class MyClass_Test {
	private static testmethod void loggerTest(){
		Test.startTest();
		try {
 				throw new Exception('testing');
			}catch (Exception ex) {
     			 MyClass.myMethod(ex);
			}
		Test.stopTest();
	}
}  

 

Error receiving 

Save error: Type cannot be constructed: Exception.  

 

Can anyone help me on this. 

 

Thanks 

Ali 

Best Answer chosen by Admin (Salesforce Developers) 
alibzafaralibzafar

This issue is solved now , just made a new class that extends exception class. 

 

public class MyException extends Exception{}

 

And than changed my test class code to , 

@isTest(seeAllData = false) 
public with sharing class MyClass_Test {
	private static testmethod void loggerTest(){
		Test.startTest();
		try {
 		     throw new MyException('testing');
		     }catch (Exception ex) {
     			 MyClass.myMethod(ex);
		     }
		Test.stopTest();
	}
} 

 

I have put the solution, so this might help any newbie. 

 

Thanks 

Ali