Saturday, March 31, 2012

Exception Handling in Javascript


Let's talk about Exception Handling in Javascript

We can use try catch and finally ver welll in our client side code The Error object in all browsers support the following two properties:
name: The name of the error, or more specifically, the name of the constructor function the error belongs to.
message: A description of the error, with this description varying depending on the browser.

inbuilt Exceptions  :
EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError
   
   
Throwing your own errors (exceptions)
   
    throw new Error("Oh oh, an error has occured");
     
    throw new SyntaxError("Your syntax is no good");
   
    throw{
 name: "JavaScriptKit Error",
 message: "Error detected. Please contact webmaster"
}
   
function addClass(element, className){
    if (element != null && typeof element.className == "string"){
        element.className += " " + className;
    } else {
        throw new Error("addClass(): First arg must be a DOM element.");
    }
}

try catch and finally exmaple:

try{
 undefinedfunction()
 alert('I guess you do exist')
}
catch(e){
 alert('An error has occurred: '+e.message)
}
finally{
 alert('I am alerted regardless of the outcome above')
}

composite primary key using one of two annotations: @IdClass or @EmbeddedId.

@Entity
@AssociationOverride( name="logRequest.fileName", joinColumns = { @JoinColumn(name="log_request_file_name") } )
public class Reporting {

    @EmbeddedId
    private ReportingFile logRequest;

    @CollectionOfElements(fetch = FetchType.EAGER)
    @JoinTable(name = "t_reports", schema="", joinColumns = {@JoinColumn(name = "log_report")})
    @Fetch(FetchMode.SELECT)
    private List<ReportingFile> reports;

    @Column(name="generated_date",nullable=true)
    private Date generatedDate;

    [...]
}

@Embeddable
public class ReportingFile {

    @Column(name="file_name",length=255)
    private String fileName;

    @Column(name="xml_content")
    private Clob xmlContent;

    [...]
}
 

No comments:

Post a Comment