Click here to Skip to main content
15,887,392 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
SQL
    function checkimgexist(){
try{
        document.getElementById('sample').src = 'res/defult/arrow.svg';
        }
   }
   catch(ex){
        cosole.log('error here is '+ex);
   }

This code works if image exists and if not, it shows error in console.
I'm not sure whether it is this exception because it don't looks like exception of the catch block!
Posted
Updated 11-Feb-15 19:56pm
v5

No, this is not correct! You need a catch block IN your function like this:


JavaScript
function checkimgexist()
    {
        try
        {
           document.getElementById('sample').src = 'res/defult/arrow.svg';
        }
        catch(ex)
        {
          cosole.log('error here is '+ex);
        }
    }
 
Share this answer
 
Make Sure the image tag is available with your name(Sample).
HTML
<img id="sample" src="" />

then try to access as your code
JavaScript
document.getElementById("sample").src


The Javascript Try block can only find error with in the HTML document, not about the Physical Path of your file(res/defult/arrow.svg).

For exp, above code works as if the image tag is available then sets source of the image tag with "res/defult/arrow.svg"
other wise it throws the error to Catch Block.
 
Share this answer
 
Code formatting is not correct. Just check the closing braces.
 
Share this answer
 
Comments
venkat teja 12-Feb-15 2:25am    
This is the currrent format the problem is catch block not able to catch file not exception(when the image doesnt exist)

function checkimgexist(){
try
{
if(document.getElementById('sample')){
document.getElementById('sample').src = 'res/defult/arrow.svg';
}
}
catch(ex)
{
cosole.log('error here is '+ex);
}
}
/\jmot 12-Feb-15 7:26am    
-1Why you are just posting comments to the answer??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900