重构代码以排除 try catch 场景,最佳实践?
Refactoring code to exclude try catch scenarios, best practices?
由于 Unity3D 通过 WebGL 在浏览器中工作的方式,try/catch 的捕捉部分会立即导致游戏崩溃。这主要是因为发布版本不支持它。 (有调试版本可以与 try/catch 一起使用,但这也会产生后果)
最后,我必须重构一些代码,使其在没有任何 try/catch 的情况下工作,以防止它为用户锁定。
这是我目前拥有的:
try{
using (StreamReader reader = new StreamReader(Application.persistentDataPath+"/"+"CHARVER.BIN"))
{
JSONNode characterImages;
string json = reader.ReadToEnd();
if(string.IsNullOrEmpty(json)){
throw new System.IO.FileNotFoundException();
}else{
characterImages = JSON.Parse(json);
//test the image file inside
Texture2D loadedImage = new Texture2D(1,1,TextureFormat.ARGB32,false);
try{
loadedImage.LoadImage(File.ReadAllBytes(Application.persistentDataPath + characterImages ["Characters"][0]["texture"]));
if(!loadedImage){
throw new System.IO.FileNotFoundException();
}
}
catch(System.Exception ex){
Debug.LogError("[DOWNLOAD]Test image file was not found: "+ex);
ThrowBackToLogin("No image file was found.");
yield break;
}
}
}
}catch(System.Exception ex){
Debug.Log("[DOWNLOAD]Test file was not found: "+ex);
ThrowBackToLogin("No version file found.");
yield break;
}
我如何重构它以删除 try-catch 部分?我不可能手动避免 File.RealAllBytes 可以抛给我的所有异常,对吧?我可以想象只调用一个 "exception message" 方法来获取一条消息并响应失败的检查和 "return;" 之后,(替换 throw new System.IO.FileNotFoundException(); )因为我抛出我自己。但是它如何适用于我无法控制的更高级的方法?
提前致谢
-笑脸
好的,这个怎么样?
using (StreamReader reader = new StreamReader(Application.persistentDataPath+"/"+"CHARVER.BIN"))
{
JSONNode characterImages;
string json = reader.ReadToEnd();
if(!string.IsNullOrEmpty(json))
{
characterImages = JSON.Parse(json);
//test the image file inside
Texture2D loadedImage = new Texture2D(1,1,TextureFormat.ARGB32,false);
if(characterImages.Keys.Contains("Characters")
&& characterImages["Characters"].Count() > 0
&& characterImages["Characters"][0].Keys.Contains("texture"))
{
if(loadedImage.LoadImage(File.ReadAllBytes(Application.persistentDataPath + characterImages["Characters"][0]["texture"])))
return;
}
}
ThrowBackToLogin("No image file was found.");
}
由于 Unity3D 通过 WebGL 在浏览器中工作的方式,try/catch 的捕捉部分会立即导致游戏崩溃。这主要是因为发布版本不支持它。 (有调试版本可以与 try/catch 一起使用,但这也会产生后果) 最后,我必须重构一些代码,使其在没有任何 try/catch 的情况下工作,以防止它为用户锁定。
这是我目前拥有的:
try{
using (StreamReader reader = new StreamReader(Application.persistentDataPath+"/"+"CHARVER.BIN"))
{
JSONNode characterImages;
string json = reader.ReadToEnd();
if(string.IsNullOrEmpty(json)){
throw new System.IO.FileNotFoundException();
}else{
characterImages = JSON.Parse(json);
//test the image file inside
Texture2D loadedImage = new Texture2D(1,1,TextureFormat.ARGB32,false);
try{
loadedImage.LoadImage(File.ReadAllBytes(Application.persistentDataPath + characterImages ["Characters"][0]["texture"]));
if(!loadedImage){
throw new System.IO.FileNotFoundException();
}
}
catch(System.Exception ex){
Debug.LogError("[DOWNLOAD]Test image file was not found: "+ex);
ThrowBackToLogin("No image file was found.");
yield break;
}
}
}
}catch(System.Exception ex){
Debug.Log("[DOWNLOAD]Test file was not found: "+ex);
ThrowBackToLogin("No version file found.");
yield break;
}
我如何重构它以删除 try-catch 部分?我不可能手动避免 File.RealAllBytes 可以抛给我的所有异常,对吧?我可以想象只调用一个 "exception message" 方法来获取一条消息并响应失败的检查和 "return;" 之后,(替换 throw new System.IO.FileNotFoundException(); )因为我抛出我自己。但是它如何适用于我无法控制的更高级的方法?
提前致谢 -笑脸
好的,这个怎么样?
using (StreamReader reader = new StreamReader(Application.persistentDataPath+"/"+"CHARVER.BIN"))
{
JSONNode characterImages;
string json = reader.ReadToEnd();
if(!string.IsNullOrEmpty(json))
{
characterImages = JSON.Parse(json);
//test the image file inside
Texture2D loadedImage = new Texture2D(1,1,TextureFormat.ARGB32,false);
if(characterImages.Keys.Contains("Characters")
&& characterImages["Characters"].Count() > 0
&& characterImages["Characters"][0].Keys.Contains("texture"))
{
if(loadedImage.LoadImage(File.ReadAllBytes(Application.persistentDataPath + characterImages["Characters"][0]["texture"])))
return;
}
}
ThrowBackToLogin("No image file was found.");
}