www.webdeveloper.com
Results 1 to 6 of 6

Thread: Slightly advanced exception handling

  1. #1
    Join Date
    Aug 2007
    Location
    London
    Posts
    410

    Slightly advanced exception handling

    Hi all,

    I'm trying to develop proper exception handling for a javascript framework I'm developing but I keep hitting an annoying dead end: caller and line numbers / stack trace.

    I have a basic exception class:
    Code:
    /**
     * Exception.js
     *
     * @class	Exception
     * 
     * The basic generic exception class.
     *
    */
    
    var Exception = new Class({
    	name: "Basic",
    	message: null,
    	
    	/**
    	 * Initialisation method.
    	 * Initialises a new exception with an error message.
    	 *
    	 * @access	public
    	 * @param	string	message.	The error message relating to the exception.
    	 * @param	boolean	log.		Whether or not to log to the console.
    	 * @return	void
    	*/
    	
    	initialize: function(message, log) {
    		this.message = message;
    		
    		if(log === true) {
    			this.log();
    		}
    	}
    });
    and an ExceptionHandler class

    Code:
    /**
     * ExceptionHandler.js
     *
     * @object	ExceptionHandler
     * Handles exceptions generated by the UIKit.
     *
    */
    
    var ExceptionHandler = {
    	
    	/**
    	 * handle method.
    	 * Handles an exception.
    	 *
    	 * @static
    	 * @param	Exception	e.	The exception to be handled.
    	 * @return	void
    	*/
    	
    	handle: function(e) {
    		//Send Notification to alert the user
    		NotificationCentre.sendNotification("exception", {title: e.name, message: e.message});
    		
    		//Log to the console
    		this.log(e);
    	},
    	
    	/**
    	 * log method.
    	 * Logs an exception to the console.
    	 *
    	 * @static
    	 * @param	Exception	e.	The exception to be handled.
    	 * @return 	void
    	*/
    	
    	log: function(e) {
    		if($chk(console)) {
    			console.error(e.name + ": \"" + e.message + "\"");
    			try {
    				console.log("Trace:");
    				console.trace();
    				console.log("[end]");
    			} catch(e) {
    				console.warn("Stack trace unavailable.");
    			}
    		}
    	}
    };
    The problem is that the console.trace() function returns only one line: "log", referring to ExceptionHandler.log(e) (at least in webkit). What I'd love to do is have the Exception class get information as to (at the very least) what called it and also, although perhaps less viable, the line number it was called on. I don't think the line number is going to work though. I would _like_ this to happen automatically, but if I have to include another argument called_from it won't be the end of the world. I know about arguments.caller but this is depreciated (as of ECMAScript 1.3 (?)).

    Any nice solutions/discussion would be much appreciated because I think this is an area in which javascript is lacking a little.

  2. #2
    Join Date
    Aug 2007
    Location
    London
    Posts
    410

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Maybe the reason for you did not get any answer is that the beginners do not understand the code while the experts do not like frameworks.

  4. #4
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    @Kor:

    on the first point: I don't really expect any beginners, on the second: I'm not sure that this is true because there are plenty of "experts" writing some really fairly impressive things using frameworks.

    The framework is, however, irrelevant. All I am really looking for is a way to get a proper trace or at least a function/line reference preferably without passing a reference manually.

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You may use Function.caller, instead of arguments.caller (which is deprecated, indeed)

  6. #6
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    Okay, thanks.

    After some testing it looks like JS is a bit limited in this respect because of the lack of a proper OO model.

    It looks like I'm going to just pass the class and method names in as a parameter.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles