- Home /
handling exceptions in JavaScript
hi
i get an unknown exception which crashes unity. I want to insert a try catch so that i can catch this exception.
the code i use is the following:
try
{
if (texture.LoadImage(System.IO.File.ReadAllBytes(cached_filename)))
{
}
else
{
return;
}
}
catch(err : System.Exception)
{
throw new System.Exception(err.Message);
Debug.Log(err.Message);
}
with this code i get a internal compile error, null ref exception in the catch line but dont know why, can anyone help me?
thanks
It will never get to your Debug.Log line, because throwing an exception will exit the function. Also, to re-throw the same exception, just use "throw" with no parameters (at least, this works in C#, hopefully javascript is the same).
Answer by Eric5h5 · Jul 19, 2010 at 09:28 AM
The point of throwing an exception is that you catch the exception in the catch block. So putting throw inside catch makes no sense.
Unless you're throwing a different exception (common practice in APIs for example). But re-throwing System.Exception is just a little weird :D