@@ -499,3 +499,80 @@ function testReturnInFinallySuppressesException(): i32 {
499499}
500500assert ( testReturnInFinallySuppressesException ( ) == 42 ) ;
501501assert ( finallyReturnSuppressedExceptionRan ) ;
502+
503+ // ============================================================
504+ // Tests for catching abort() and runtime errors
505+ // ============================================================
506+
507+ // Test catching abort()
508+ function testCatchAbort ( ) : bool {
509+ let caught = false ;
510+ try {
511+ abort ( "this should be catchable" ) ;
512+ } catch ( e ) {
513+ caught = true ;
514+ // Verify we got an Error with the abort message
515+ assert ( e . message . includes ( "this should be catchable" ) ) ;
516+ }
517+ return caught ;
518+ }
519+ assert ( testCatchAbort ( ) ) ;
520+
521+ // Test catching runtime errors from __new (allocation too large)
522+ function testCatchRuntimeError ( ) : bool {
523+ let caught = false ;
524+ try {
525+ // Try to allocate an impossibly large object
526+ // This should trigger an allocation error in the runtime
527+ __new ( usize . MAX_VALUE , idof < ArrayBuffer > ( ) ) ;
528+ } catch ( e ) {
529+ caught = true ;
530+ // Should have caught an allocation error
531+ assert ( e . message . length > 0 ) ;
532+ }
533+ return caught ;
534+ }
535+ assert ( testCatchRuntimeError ( ) ) ;
536+
537+ // Test that abort in a function can be caught by the caller
538+ function functionThatAborts ( ) : void {
539+ abort ( "abort from function" ) ;
540+ }
541+
542+ function testCatchAbortFromFunction ( ) : bool {
543+ let caught = false ;
544+ try {
545+ functionThatAborts ( ) ;
546+ } catch ( e ) {
547+ caught = true ;
548+ assert ( e . message . includes ( "abort from function" ) ) ;
549+ }
550+ return caught ;
551+ }
552+ assert ( testCatchAbortFromFunction ( ) ) ;
553+
554+ // Test catch variable is properly typed as Error
555+ function testCatchVariableType ( ) : bool {
556+ try {
557+ throw new Error ( "type test" ) ;
558+ } catch ( e ) {
559+ // e should be typed as Error, so we can access message directly
560+ let msg : string = e . message ;
561+ return msg == "type test" ;
562+ }
563+ return false ;
564+ }
565+ assert ( testCatchVariableType ( ) ) ;
566+
567+ // Test catching custom Error subclass (use existing CustomError class)
568+ function testCatchCustomError2 ( ) : bool {
569+ try {
570+ throw new CustomError ( "custom error 2" , 99 ) ;
571+ } catch ( e ) {
572+ // e is typed as Error, need to cast to access code
573+ let custom = e as CustomError ;
574+ return custom . message == "custom error 2" && custom . code == 99 ;
575+ }
576+ return false ;
577+ }
578+ assert ( testCatchCustomError2 ( ) ) ;
0 commit comments